-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathssh.go
240 lines (204 loc) · 4.77 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package ppsh
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"strings"
"time"
"golang.org/x/crypto/ssh"
)
var (
defaultCiphers = []string{
"aes128-ctr",
"aes192-ctr",
"aes256-ctr",
"aes128-gcm@openssh.com",
"arcfour256",
"arcfour128",
"aes128-cbc",
"3des-cbc",
"aes192-cbc",
"aes256-cbc",
}
defaultHost = "localhost"
defaultUser = "root"
defaultTimeout = 60 * 5
defaultPort = 22
defaultPlatform = "linux"
)
func validatedCiphers(ciphers []string) []string {
if len(ciphers) == 0 {
return defaultCiphers
}
return ciphers
}
func getSSHAuthMethod(password, certKey string) ([]ssh.AuthMethod, error) {
// both password and cert key is null
if password == "" && certKey == "" {
return nil, fmt.Errorf("password and certKey cannot be null at the some time")
}
auth := make([]ssh.AuthMethod, 0)
// only password
if certKey == "" {
auth = append(auth, ssh.Password(password))
return auth, nil
}
// read the cert key data
pemBytes, err := ioutil.ReadFile(certKey)
if err != nil {
return nil, err
}
var signer ssh.Signer
if password != "" {
// both password and cert key
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(password))
} else {
// only cert key
signer, err = ssh.ParsePrivateKey(pemBytes)
}
if err != nil {
return nil, err
}
auth = append(auth, ssh.PublicKeys(signer))
return auth, nil
}
func connect(user, password, host, certKey string, port, timeout int, ciphers []string) (*ssh.Session, error) {
// set ssh auth method
auth, err := getSSHAuthMethod(password, certKey)
if err != nil {
return nil, err
}
// set ssh config
clientConfig := &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: time.Duration(timeout) * time.Second,
Config: ssh.Config{
Ciphers: validatedCiphers(ciphers),
},
HostKeyCallback: func(host string, remote net.Addr, certKey ssh.PublicKey) error {
return nil
},
}
// dial to ssh server and get the client
addr := fmt.Sprintf("%s:%d", host, port)
client, err := ssh.Dial("tcp", addr, clientConfig)
if err != nil {
return nil, err
}
// create ssh session
session, err := client.NewSession()
if err != nil {
return nil, err
}
// simulate terminal login
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
if err := session.RequestPty("xterm", 100, 180, modes); err != nil {
return nil, err
}
return session, nil
}
func doOther(user, password, host, certKey string, port, timeout int, ciphers, cmds []string, srch chan Result) {
r := Result{Host: host}
session, err := connect(user, password, host, certKey, port, timeout, ciphers)
if err != nil {
r.Error = err.Error()
srch <- r
return
}
defer session.Close()
var outBuf, errBuf bytes.Buffer
session.Stdout = &outBuf
session.Stderr = &errBuf
if err = session.Shell(); err != nil {
r.Error = err.Error()
srch <- r
return
}
cmds = append(cmds, "exit") // remember to exit
stdinBuf, _ := session.StdinPipe()
for _, c := range cmds {
c = c + "\n"
r.Cmd += c
stdinBuf.Write([]byte(c))
}
session.Wait()
if errBuf.String() != "" {
r.Error = errBuf.String()
} else {
r.Success = true
r.Detail = outBuf.String()
}
srch <- r
return
}
func doLinux(user, password, host, certKey string, port, timeout int, ciphers, cmds []string, srch chan Result) {
cmds = append(cmds, "exit")
cmd := strings.Join(cmds, " && ")
r := Result{Host: host, Cmd: cmd}
session, err := connect(user, password, host, certKey, port, timeout, ciphers)
if err != nil {
r.Error = err.Error()
r.Code = -1
srch <- r
return
}
defer session.Close()
var outBuf, errBuf bytes.Buffer
{
session.Stdout = &outBuf
session.Stderr = &errBuf
}
if err := session.Run(cmd); err != nil {
r.Error = err.Error()
srch <- r
return
}
if errBuf.String() != "" {
r.Error = errBuf.String()
} else {
r.Success = true
r.Detail = outBuf.String()
}
srch <- r
return
}
func Do(user, password, host, key string, port int, timeout int, ciphers, cmds []string, flatform string, ch chan Result) {
if host == "" {
host = defaultHost
}
if user == "" {
user = defaultUser
}
if timeout == 0 {
timeout = defaultTimeout
}
if port == 0 {
port = defaultPort
}
if flatform == "" {
flatform = defaultPlatform
}
srch := make(chan Result)
if flatform == "linux" {
go doLinux(user, password, host, key, port, timeout, ciphers, cmds, srch)
} else {
go doOther(user, password, host, key, port, timeout, ciphers, cmds, srch)
}
r := Result{Host: host}
select {
case <-time.After(time.Duration(timeout) * time.Second):
ch <- Result{
Host: host,
Error: fmt.Sprintf("ssh run timeout in %d second", timeout),
}
case r = <-srch:
ch <- r
}
return
}