-
Notifications
You must be signed in to change notification settings - Fork 34
/
ssh.go
122 lines (110 loc) · 3.88 KB
/
ssh.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
package ssh
import (
"fmt"
"strings"
"time"
)
const (
HUAWEI = "huawei"
H3C = "h3c"
CISCO = "cisco"
)
var IsLogDebug = true
/**
* 外部调用的统一方法,完成获取会话(若不存在,则会创建连接和会话,并存放入缓存),执行指令的流程,返回执行结果
* @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口, cmds 执行的指令(可以多个)
* @return 执行的输出结果和执行错误
* @author shenbowei
*/
func RunCommands(user, password, ipPort string, cmds ...string) (string, error) {
sessionKey := user + "_" + password + "_" + ipPort
sessionManager.LockSession(sessionKey)
defer sessionManager.UnlockSession(sessionKey)
sshSession, err := sessionManager.GetSession(user, password, ipPort, "")
if err != nil {
LogError("GetSession error:%s", err)
return "", err
}
sshSession.WriteChannel(cmds...)
result := sshSession.ReadChannelTiming(2 * time.Second)
filteredResult := filterResult(result, cmds[0])
return filteredResult, nil
}
/**
* 外部调用的统一方法,完成获取会话(若不存在,则会创建连接和会话,并存放入缓存),执行指令的流程,返回执行结果
* @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口, brand 交换机品牌(可为空), cmds 执行的指令(可以多个)
* @return 执行的输出结果和执行错误
* @author shenbowei
*/
func RunCommandsWithBrand(user, password, ipPort, brand string, cmds ...string) (string, error) {
sessionKey := user + "_" + password + "_" + ipPort
sessionManager.LockSession(sessionKey)
defer sessionManager.UnlockSession(sessionKey)
sshSession, err := sessionManager.GetSession(user, password, ipPort, brand)
if err != nil {
LogError("GetSession error:%s", err)
return "", err
}
sshSession.WriteChannel(cmds...)
result := sshSession.ReadChannelTiming(2 * time.Second)
filteredResult := filterResult(result, cmds[0])
return filteredResult, nil
}
/**
* 外部调用的统一方法,完成获取交换机的型号
* @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口
* @return 设备品牌(huawei,h3c,cisco,"")和执行错误
* @author shenbowei
*/
func GetSSHBrand(user, password, ipPort string) (string, error) {
sessionKey := user + "_" + password + "_" + ipPort
sessionManager.LockSession(sessionKey)
defer sessionManager.UnlockSession(sessionKey)
sshSession, err := sessionManager.GetSession(user, password, ipPort, "")
if err != nil {
LogError("GetSession error:%s", err)
return "", err
}
return sshSession.GetSSHBrand(), nil
}
/**
* 对交换机执行的结果进行过滤
* @paramn result:返回的执行结果(可能包含脏数据), firstCmd:执行的第一条指令
* @return 过滤后的执行结果
* @author shenbowei
*/
func filterResult(result, firstCmd string) string {
//对结果进行处理,截取出指令后的部分
filteredResult := ""
resultArray := strings.Split(result, "\n")
findCmd := false
promptStr := ""
for _, resultItem := range resultArray {
resultItem = strings.Replace(resultItem, " \b", "", -1)
if findCmd && (promptStr == "" || strings.Replace(resultItem, promptStr, "", -1) != "") {
filteredResult += resultItem + "\n"
continue
}
if strings.Contains(resultItem, firstCmd) {
findCmd = true
promptStr = resultItem[0:strings.Index(resultItem, firstCmd)]
promptStr = strings.Replace(promptStr, "\r", "", -1)
promptStr = strings.TrimSpace(promptStr)
LogDebug("Find promptStr='%s'", promptStr)
//将命令添加到结果中
filteredResult += resultItem + "\n"
}
}
if !findCmd {
return result
}
return filteredResult
}
func LogDebug(format string, a ...interface{}) {
if IsLogDebug {
fmt.Println("[DEBUG]:" + fmt.Sprintf(format, a...))
}
}
func LogError(format string, a ...interface{}) {
fmt.Println("[ERROR]:" + fmt.Sprintf(format, a...))
}