/
ssh.go
105 lines (96 loc) · 2.48 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 ssh
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
"github.com/wanglu119/ng-ssh/lib/common"
)
func NewSshClient(mc *common.Machine) (*ssh.Client, error) {
config := &ssh.ClientConfig{
Timeout: time.Second * 5,
User: mc.User,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //这个可以, 但是不够安全
//HostKeyCallback: hostKeyCallBackFunc(mc.Host),
}
if mc.Type == "password" {
config.Auth = []ssh.AuthMethod{ssh.Password(mc.Password)}
} else {
config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(mc.Key)}
}
addr := fmt.Sprintf("%s:%d", mc.Host, mc.Port)
c, err := ssh.Dial("tcp", addr, config)
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.Error(fmt.Sprintf("find known_hosts's home dir failed: %v", err))
}
file, err := os.Open(hostPath)
if err != nil {
log.Error(fmt.Sprintf("can't find known_host file: %v", 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.Error(fmt.Sprintf("error parsing %q: %v", fields[2], err))
}
break
}
}
if hostKey == nil {
log.Error(fmt.Sprintf("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.Error(fmt.Sprintf("find key's home dir failed: %v", err))
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
log.Error(fmt.Sprintf("ssh key file read failed: %v", err))
}
// CreateUserOfRole the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Error(fmt.Sprintf("ssh key signer failed: %v", err))
}
return ssh.PublicKeys(signer)
}
func runCommand(client *ssh.Client, command string) (stdout string, err error) {
session, err := client.NewSession()
if err != nil {
log.Error(fmt.Sprintf("%v", 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
}