forked from gaoyue1989/sshexec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
195 lines (159 loc) · 3.77 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
package sshexec
import (
"bytes"
"errors"
"fmt"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"net"
"os"
"time"
)
// ssh session
type HostSession struct {
Username string
Password string
Hostname string
Signers []ssh.Signer
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
}
// 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, &config)
if err != nil {
result.Error = err
return result
}
session, err := client.NewSession()
if err != nil {
result.Error = err
return result
}
defer session.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.Result = b1.String()
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, &config)
if err != nil {
result.Error = err
return result
}
session, err := client.NewSession()
if err != nil {
result.Error = err
return result
}
defer session.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.Error = err
return result
}
defer srcFile.Close()
sftpClient, err := sftp.NewClient(client)
// 这里换成实际的 SSH 连接的 用户名,密码,主机名或IP,SSH端口
// create sftp client
if err != nil {
result.Error = err
return result
}
defer sftpClient.Close()
dstFile, err := sftpClient.Create(remoteFilePath)
if err != nil {
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.Error = err
return result
}
if n != fileSize {
result.Error = errors.New(fmt.Sprintf("copy: expected %v bytes, got %d", fileSize, n))
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,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
//config.Ciphers = []string{"aes128-cbc", "3des-cbc"}
return config
}
func readKey(filename string) (ssh.Signer, error) {
f, _ := os.Open(filename)
bytesStream, _ := ioutil.ReadAll(f)
return generateKey(bytesStream)
}
func generateKey(keyData []byte) (ssh.Signer, error) {
return ssh.ParsePrivateKey(keyData)
}