-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuawei_ssh_channel.go
150 lines (130 loc) · 3.62 KB
/
huawei_ssh_channel.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
package main
import (
"fmt"
"io/ioutil"
"golang.org/x/crypto/ssh"
"time"
"bytes"
"net"
"strings"
)
const (
username = "admin"
password = "123456"
network_ipaddress = "10.10.10.100"
port_number = 22
commands = "display cpu;display mem;display current-configuration" //多条命令用 ; 号隔开
)
var (
ciphers_list = []string{"[email protected]", "arcfour256", "aes128-ctr", "aes192-ctr", "aes256-ctr", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"}
) //加密方法,可以继续添加 避免对设备不支持
func main() {
shell_channel, err := connect(username, password, network_ipaddress, "" , port_number, ciphers_list)
if err != nil {
fmt.Println("连接创建错误 : ",err)
return
}
defer session.Close()
cmd_list := strings.Split(commands, ";")
stdinBuf, err := shell_channel.StdinPipe()
if err != nil {
fmt.Println("通道无法进行操作 : ",err)
return
}
var outbt, errbt bytes.Buffer
shell_channel.Stdout = &outbt
shell_channel.Stderr = &errbt
err = shell_channel.Shell()
if err != nil {
fmt.Println(err)
return
}
for _, cmd := range cmd_list {
cmd = cmd + "\n"
stdinBuf.Write([]byte(cmd))
for {
time.Sleep(time.Second * 1)
if strings.HasSuffix(outbt.String(),"---- More ----") { //完整显示命令结果处理 ---- More ---- 方法
stdinBuf.Write([]byte(" \n"))
} else {
break
}
}
time.Sleep(time.Second * 1)
}
stdinBuf.Write(huawei_quit())
session.Wait()
fmt.Println(outbt.String() + errbt.String())
}
func huawei_quit() []byte { //华为交换机需要按空格继续
quit_command := []byte("quit\n")
return quit_command
}
//使用 https://xiaozhuanlan.com/topic/3490681275 描述方法
func connect(user, password, host, key string, port int, cipherList []string) (*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)
if key == "" {
auth = append(auth, ssh.Password(password))
} else {
pemBytes, err := ioutil.ReadFile(key)
if err != nil {
return nil, err
}
var signer ssh.Signer
if password == "" {
signer, err = ssh.ParsePrivateKey(pemBytes)
} else {
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(password))
}
if err != nil {
return nil, err
}
auth = append(auth, ssh.PublicKeys(signer))
}
if len(cipherList) == 0 {
config = ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "[email protected]", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
}
} else {
config = ssh.Config{
Ciphers: cipherList,
}
}
clientConfig = &ssh.ClientConfig{
User: user,
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", host, 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
}