/
ssh.go
105 lines (99 loc) · 2.38 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package core
import (
"bufio"
"bytes"
"fmt"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"os"
"strings"
"time"
//"github.com/pkg/sftp"
)
func NewSshClient(server Server) (*ssh.Client, error) {
config := &ssh.ClientConfig{
Timeout: time.Second * 5,
User: server.User,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //这个可以, 但是不够安全
//HostKeyCallback: hostKeyCallBackFunc(h.Host),
}
//if h.Type == "password" {
config.Auth = []ssh.AuthMethod{ssh.Password(server.Passwd)}
//} else {
// config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(h.Key)}
//}
addr := fmt.Sprintf("%s:%d", server.Ip, server.Port)
c, err := ssh.Dial("tcp", addr, config)
//s,_ := sftp.NewClient(c)
//s.Write()
if err != nil {
return nil, err
}
return c, nil
}
func hostKeyCallBackFunc(host string) ssh.HostKeyCallback {
hostPath, err := homedir.Expand("~/.ssh/known_hosts")
if err != nil {
log.Fatal("find known_hosts's home dir failed", err)
}
file, err := os.Open(hostPath)
if err != nil {
log.Fatal("can't find known_host file:", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var hostKey ssh.PublicKey
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if len(fields) != 3 {
continue
}
if strings.Contains(fields[0], host) {
var err error
hostKey, _, _, _, err = ssh.ParseAuthorizedKey(scanner.Bytes())
if err != nil {
log.Fatalf("error parsing %q: %v", fields[2], err)
}
break
}
}
if hostKey == nil {
log.Fatalf("no hostkey for %s,%v", host, err)
}
return ssh.FixedHostKey(hostKey)
}
func publicKeyAuthFunc(kPath string) ssh.AuthMethod {
keyPath, err := homedir.Expand(kPath)
if err != nil {
log.Fatal("find key's home dir failed", err)
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
log.Fatal("ssh key file read failed", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatal("ssh key signer failed", err)
}
return ssh.PublicKeys(signer)
}
func runCommand(client *ssh.Client, command string) (stdout string, err error) {
session, err := client.NewSession()
if err != nil {
//log.Print(err)
return
}
defer session.Close()
var buf bytes.Buffer
session.Stdout = &buf
err = session.Run(command)
if err != nil {
//log.Print(err)
return
}
stdout = string(buf.Bytes())
return
}