/
ssh_client.go
199 lines (165 loc) · 3.91 KB
/
ssh_client.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
package sshrunner
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"runner"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)
type SSHClient struct {
User string
Password string
SSHKeyPath string
Host string
Port int
client *ssh.Client
session *ssh.Session
}
func NewSSHClient(user, password, sshKeyPath, host string, port int) *SSHClient {
if port == 0 {
port = 22
}
return &SSHClient{
User: user,
Password: password,
SSHKeyPath: sshKeyPath,
Host: host,
Port: port,
}
}
// Close release resources
func (sc *SSHClient) Close() {
if sc.session != nil {
sc.session.Close()
}
if sc.client != nil {
sc.client.Close()
}
}
// ExecNointeractiveCmd exec command without interactive
func (sc *SSHClient) ExecNointeractiveCmd(cmd string, timeout time.Duration) (status runner.OutputStaus, stdout, stderr *bytes.Buffer, err error) {
status = runner.Fail
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}
var errChan = make(chan error)
// create session
if err = sc.createSession(); err != nil {
status = runner.Timeout
return
}
defer sc.Close()
sc.session.Stdout = stdout
sc.session.Stderr = stderr
go func(session *ssh.Session) {
if err = session.Start(cmd); err != nil {
errChan <- err
}
if err = session.Wait(); err != nil {
errChan <- err
}
errChan <- nil
}(sc.session)
select {
case err = <-errChan:
case <-time.After(timeout):
err = fmt.Errorf("exec command(%s) on host(%s) TIMEOUT", cmd, sc.Host)
status = runner.Timeout
}
if err == nil {
status = runner.Success
}
return
}
// ExecInteractiveCmd exec command with interactive
func (sc *SSHClient) ExecInteractiveCmd(cmd string) error {
var err error
// create session
if err = sc.createSession(); err != nil {
return err
}
defer sc.Close()
fd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(fd)
if err != nil {
panic(err)
}
defer terminal.Restore(fd, oldState)
// excute command
sc.session.Stdout = os.Stdout
sc.session.Stderr = os.Stderr
sc.session.Stdin = os.Stdin
termWidth, termHeight, err := terminal.GetSize(fd)
if err != nil {
panic(err)
}
// Set up terminal modes
modes := ssh.TerminalModes{
ssh.ECHO: 1, // enable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := sc.session.RequestPty("xterm-256color", termHeight, termWidth, modes); err != nil {
return err
}
if err := sc.session.Run(cmd); err != nil {
return err
}
return nil
}
// createClient create ssh client
func (sc *SSHClient) createClient() error {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
err error
)
// get auth method
auth, _ = authMethods(sc.Password, sc.SSHKeyPath)
clientConfig = &ssh.ClientConfig{
User: sc.User,
Auth: auth,
Timeout: 30 * time.Second,
}
// connet to ssh
addr = fmt.Sprintf("%s:%d", sc.Host, sc.Port)
if sc.client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return err
}
return nil
}
// createSession create session for ssh use
func (sc *SSHClient) createSession() error {
var err error
// create client
if err = sc.createClient(); err != nil {
return err
}
// create session
if sc.session, err = sc.client.NewSession(); err != nil {
return err
}
return nil
}
// authMethods get auth methods
func authMethods(password, sshKeyPath string) ([]ssh.AuthMethod, error) {
var (
err error
authkey []byte
signer ssh.Signer
authMethods = make([]ssh.AuthMethod, 0)
)
authMethods = append(authMethods, ssh.Password(password))
if authkey, err = ioutil.ReadFile(sshKeyPath); err != nil {
return authMethods, err
}
if signer, err = ssh.ParsePrivateKey(authkey); err != nil {
return authMethods, err
}
authMethods = append(authMethods, ssh.PublicKeys(signer))
return authMethods, nil
}