/
session.go
220 lines (185 loc) · 4.71 KB
/
session.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
package sshexec
import (
"bytes"
"errors"
"fmt"
"github.com/astaxie/beego"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"os"
"strconv"
"time"
)
// ssh session
type HostSession struct {
Username string
Password string
Hostname string
Signers []ssh.Signer
Port int
Auths []ssh.AuthMethod
}
// result of the command execution
type ExecResult struct {
Id int
Host string
Command string
LocalFilePath string
RemoteFilePath string
Result string
StartTime time.Time
EndTime time.Time
Error error
ErrorInfo string
}
// execute the command and return a result structure
func (exec *HostSession) Exec(id int, command string, config ssh.ClientConfig) *ExecResult {
result := &ExecResult{
Id: id,
Host: exec.Hostname,
Command: command,
}
client, err := ssh.Dial("tcp", exec.Hostname+":"+strconv.Itoa(exec.Port), &config)
if err != nil {
result.Error = err
result.ErrorInfo = err.Error()
result.Result = "ssh连接出错,请检查key信任或ip是否正确"
return result
}
session, err := client.NewSession()
if err != nil {
result.ErrorInfo = err.Error()
result.Result = "ssh连接出错,请检查key信任或ip是否正确"
result.Error = err
return result
}
defer func() {
session.Close()
client.Close()
}()
var b bytes.Buffer
session.Stdout = &b
var b1 bytes.Buffer
session.Stderr = &b1
start := time.Now()
if err := session.Run(command); err != nil {
result.Error = err
result.ErrorInfo = err.Error()
result.Result = b1.String() + " 错误信息:" + err.Error()
return result
}
end := time.Now()
result.Result = b.String()
result.StartTime = start
result.EndTime = end
return result
}
// execute the command and return a result structure
func (exec *HostSession) Transfer(id int, localFilePath string, remoteFilePath string, config ssh.ClientConfig) *ExecResult {
result := &ExecResult{
Id: id,
Host: exec.Hostname,
LocalFilePath: localFilePath,
RemoteFilePath: remoteFilePath,
}
start := time.Now()
result.StartTime = start
client, err := ssh.Dial("tcp", exec.Hostname+":"+strconv.Itoa(exec.Port), &config)
if err != nil {
result.ErrorInfo = err.Error()
result.Result = "ssh连接出错,请检查key信任或ip是否正确"
result.Error = err
return result
}
session, err := client.NewSession()
if err != nil {
result.ErrorInfo = err.Error()
result.Result = "ssh连接出错,请检查key信任或ip是否正确"
result.Error = err
return result
}
defer func() {
session.Close()
client.Close()
}()
var fileSize int64
if s, err := os.Stat(localFilePath); err != nil {
result.Error = err
return result
} else {
fileSize = s.Size()
}
srcFile, err := os.Open(localFilePath)
if err != nil {
result.ErrorInfo = err.Error()
result.Result = err.Error()
result.Error = err
return result
}
defer srcFile.Close()
sftpClient, err := sftp.NewClient(client)
// 这里换成实际的 SSH 连接的 用户名,密码,主机名或IP,SSH端口
// create sftp client
if err != nil {
result.ErrorInfo = err.Error()
result.Result = err.Error()
result.Error = err
return result
}
defer sftpClient.Close()
dstFile, err := sftpClient.Create(remoteFilePath)
if err != nil {
result.ErrorInfo = err.Error()
result.Result = err.Error()
result.Error = err
return result
}
defer dstFile.Close()
//todo 这里使用buff池 或io.Copy 性能没有测试出差距
//n, err := Copy(dstFile, io.LimitReader(srcFile, fileSize))
n, err := io.Copy(dstFile, io.LimitReader(srcFile, fileSize))
if err != nil {
result.ErrorInfo = err.Error()
result.Result = err.Error()
result.Error = err
return result
}
if n != fileSize {
beego.Info(err)
result.Error = errors.New(fmt.Sprintf("copy: expected %v bytes, got %d", fileSize, n))
result.ErrorInfo = result.Error.Error()
result.Result = result.Error.Error()
return result
}
end := time.Now()
result.EndTime = end
return result
}
func (exec *HostSession) GenerateConfig() ssh.ClientConfig {
var auths []ssh.AuthMethod
if len(exec.Password) != 0 {
auths = append(auths, ssh.Password(exec.Password))
} else {
if len(exec.Auths) > 0 {
auths = exec.Auths
} else {
auths = append(auths, ssh.PublicKeys(exec.Signers...))
}
}
config := ssh.ClientConfig{
User: exec.Username,
Auth: auths,
}
config.Ciphers = []string{"aes128-cbc", "3des-cbc", "aes128-ctr", "aes192-ctr", "aes256-ctr"}
return config
}
func readKey(filename string) (ssh.Signer, error) {
f, _ := os.Open(filename)
bytes, _ := ioutil.ReadAll(f)
return generateKey(bytes)
}
func generateKey(keyData []byte) (ssh.Signer, error) {
return ssh.ParsePrivateKey(keyData)
}