-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_connect.go
182 lines (153 loc) · 3.42 KB
/
ssh_connect.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
package models
import (
"bytes"
"fmt"
"github.com/astaxie/beego/orm"
"golang.org/x/crypto/ssh"
"net"
"time"
)
type SshHost struct {
Id int
Host string `orm:"size(32)"`
Port int
Username string `orm:"size(32)"`
Password string `orm:"size(64)"`
}
type SshResult struct {
Id int
Host string
Success string
Type string
Content string
Result string
Time time.Time
}
func NewSshHost(h,u,p string,port int) *SshHost{
return &SshHost{
Host:h,
Username:u,
Password:p,
Port:port,
}
}
func AddSSHHost(s *SshHost) error{
ormer := orm.NewOrm()
_, e := ormer.Insert(s)
if e!=nil{
return e
}
return nil
}
func AddSshResult(s *SshResult) error{
ormer := orm.NewOrm()
_, e := ormer.Insert(s)
if e!=nil{
return e
}
return nil
}
func DeleteSshHost(s *SshHost){
ormer := orm.NewOrm()
ormer.Delete(s)
}
func GetSshHost(id int) *SshHost{
sshhost := &SshHost{
Id:id,
}
o :=orm.NewOrm()
o.Read(sshhost,"Id")
return sshhost
}
func GetAllSshHost() ([]*SshHost){
var hosts []*SshHost
o :=orm.NewOrm()
o.QueryTable("SshHost").All(&hosts)
return hosts
}
func GetAllSshResult() ([]*SshResult){
var results []*SshResult
o :=orm.NewOrm()
o.QueryTable("SshResult").All(&results)
return results
}
func Connect(h *SshHost) (*ssh.Session, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
client *ssh.Client
config ssh.Config
session *ssh.Session
err error
)
// get auth method
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(h.Password))
var signer ssh.Signer
auth = append(auth, ssh.PublicKeys(signer))
config = ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
}
clientConfig = &ssh.ClientConfig{
User: h.Username,
Auth: auth,
Timeout: 30 * time.Second,
Config: config,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
// connet to ssh
addr = fmt.Sprintf("%s:%d", h.Host, h.Port)
if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}
// create session
if session, err = client.NewSession(); err != nil {
return nil, err
}
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", 80, 40, modes); err != nil {
return nil, err
}
return session, nil
}
func ExecCommands(host *SshHost,session *ssh.Session,cmdlist []string) (ret *SshResult){
ret = &SshResult{}
ret.Host = host.Host
defer session.Close()
//获取ssh客户端的输入句柄
stdinBuf, err := session.StdinPipe()
if err != nil {
ret.Success = "失败"
return
}
var outbt, errbt bytes.Buffer
//获取ssh客户端的标准输出
session.Stdout = &outbt
//获取ssh客户端的标准错误
session.Stderr = &errbt
err = session.Shell()
if err != nil {
ret.Success = "失败"
return
}
//在命令后面追加exit
cmdlist = append(cmdlist,"exit")
for _, c := range cmdlist {
c = c + "\n"
stdinBuf.Write([]byte(c))
}
session.Wait()
ret.Success = "成功"
ret.Result = outbt.String() + errbt.String()
ret.Time = time.Now()
ret.Type = "命令执行"
ret.Content = "ls"
return
}