forked from gaochao1/sw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
description.go
335 lines (320 loc) · 8.24 KB
/
description.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package sw
import (
"fmt"
"github.com/gosnmp/gosnmp"
"strconv"
"strings"
"sync"
)
// VendorMap 全局记录设备型号的map
var (
VendorMap sync.Map
)
const (
H3C_V5 = "H3C_V5"
H3C_V7 = "H3C_V7"
H3C_S9500 = "H3C_S9500"
H3C_S5500 = "H3C_S5500"
H3C_V3_1 = "H3C_V3.1"
H3C_ER = "H3C_ER"
H3C_S5024P = "H3C_S5024P"
H3C_S2126T = "H3C_S2126T"
H3C = "H3C"
Cisco_NX = "Cisco_NX"
Cisco_ASA_OLD = "Cisco_ASA_OLD"
Cisco_ASA = "Cisco_ASA"
Cisco_IOS_XE = "Cisco_IOS_XE"
Cisco_IOS_XR = "Cisco_IOS_XR"
Cisco_old = "Cisco_old"
Cisco = "Cisco"
Huawei_ME60 = "Huawei_ME60"
Huawei_V5 = "Huawei_V5"
Huawei_V5_170 = "Huawei_V5.170" //S5720 S7700 S5730 S5735
Huawei_V5_150 = "Huawei_V5.150" //S5700 V200R005C10SPC50
Huawei_V5_130 = "Huawei_V5.130" //S5700 V200R003C10SPC00
Huawei_V5_70 = "Huawei_V5.70" //S2326 S2700
Huawei_V3_10 = "Huawei_V3.10"
Huawei = "Huawei"
Ruijie = "Ruijie"
Ruijie_S1900 = "Ruijie_S1900"
Ruijie_S5700 = "Ruijie_S5700"
Juniper = "Juniper"
Dell = "Dell"
Draytek = "Draytek"
FortiGate = "FortiGate"
Sundray = "Sundray"
Linux = "Linux"
FutureMatrix = "FutureMatrix"
)
func SystemName(ip, community string, retry int, timeout int) (name string, err error) {
oid := "1.3.6.1.2.1.1.5.0"
method := snmpGet
var snmpPDUs []gosnmp.SnmpPDU
snmpPDUs, err = RunSnmp(ip, community, oid, method, retry, timeout)
for _, pdu := range snmpPDUs {
if len(string(pdu.Value.([]byte))) > 0 {
name = string(pdu.Value.([]byte))
break
}
}
return
}
func SysDescription(ip, community string, retry int, timeout int) (desc string, err error) {
//sysDescr
oid := "1.3.6.1.2.1.1.1.0"
method := snmpGet
var snmpPDUs []gosnmp.SnmpPDU
snmpPDUs, err = RunSnmp(ip, community, oid, method, retry, timeout)
if err != nil {
return
}
if len(snmpPDUs) == 0 || string(snmpPDUs[0].Value.([]byte)) == "" {
//sysContact
oid = "1.3.6.1.2.1.1.4.0"
snmpPDUs, err = RunSnmp(ip, community, oid, method, retry, timeout)
if err != nil {
return
}
}
for _, pdu := range snmpPDUs {
if pdu.Value != nil && len(string(pdu.Value.([]byte))) > 0 {
desc = desc + "\n" + string(pdu.Value.([]byte))
}
}
return
}
func SerialNumber(ip, community string, retry int, timeout int) (sn string, err error) {
oid := "1.3.6.1.2.1.47.1.1.1.1.11"
method := snmpBulkWalk
var snmpPDUs []gosnmp.SnmpPDU
snmpPDUs, err = RunSnmp(ip, community, oid, method, retry, timeout)
if err != nil {
return
}
if len(snmpPDUs) == 0 {
//信锐
oid = ".1.3.6.1.4.1.45577.5.7.6.0"
snmpPDUs, err = RunSnmp(ip, community, oid, method, retry, timeout)
if err != nil {
return
}
}
for _, pdu := range snmpPDUs {
if pdu.Value != nil && len(string(pdu.Value.([]byte))) > 0 {
sn = string(pdu.Value.([]byte))
break
}
}
return
}
func SoftwareVersion(ip, community string, retry int, timeout int) (software string) {
var chSnmpPDUs = make(chan []gosnmp.SnmpPDU)
limitCh := make(chan bool, 1)
limitCh <- true
go RunSnmpRetry(ip, community, timeout, chSnmpPDUs, retry, limitCh, false, []string{"1.3.6.1.2.1.47.1.1.1.1.10", "1.3.6.1.4.1.45577.5.7.7.0"})
var s []string
snmpPDUs := <-chSnmpPDUs
for _, pdu := range snmpPDUs {
if pdu.Value != nil && len(string(pdu.Value.([]byte))) > 0 {
s = append(s, string(pdu.Value.([]byte)))
}
}
software = strings.Join(RemoveDuplicateElement(s), " ")
return
}
func ProductClass(ip, community string, retry int, timeout int) (productClass string) {
var chSnmpPDUs = make(chan []gosnmp.SnmpPDU)
limitCh := make(chan bool, 1)
limitCh <- true
go RunSnmpRetry(ip, community, timeout, chSnmpPDUs, retry, limitCh, true, []string{"1.3.6.1.2.1.47.1.1.1.1.2", "1.3.6.1.4.1.45577.5.7.8.0"})
snmpPDUs := <-chSnmpPDUs
for _, pdu := range snmpPDUs {
if pdu.Value != nil && len(string(pdu.Value.([]byte))) > 3 {
productClass = string(pdu.Value.([]byte))
break
}
}
return
}
func SysVendor(ip, community string, retry int, timeout int) (version string, sysDesc string, err error) {
sysDesc, err = SysDescription(ip, community, retry, timeout)
if err != nil {
return
}
version, err = VersionDetect(sysDesc)
return
}
func VersionDetect(sysDesc string) (version string, err error) {
sysDescLower := strings.ToLower(sysDesc)
if strings.Contains(sysDescLower, "cisco nx-os") {
version = Cisco_NX
return
}
if strings.Contains(sysDesc, "Cisco Internetwork Operating System Software") {
version = Cisco_old
return
}
if strings.Contains(sysDescLower, "cisco ios") {
if strings.Contains(sysDesc, "IOS-XE Software") {
version = Cisco_IOS_XE
return
} else if strings.Contains(sysDesc, "Cisco IOS XR") {
version = Cisco_IOS_XR
return
} else {
version = Cisco
return
}
}
if strings.Contains(sysDescLower, "cisco adaptive security appliance") {
var versionNumber float64
versionNumber, err = strconv.ParseFloat(getVersionNumber(sysDesc), 32)
if err == nil && versionNumber < 9.2 {
version = Cisco_ASA_OLD
return
}
version = Cisco_ASA
return
}
if strings.Contains(sysDescLower, "h3c") {
if strings.Contains(sysDesc, "Software Version 5") {
version = H3C_V5
return
}
if strings.Contains(sysDesc, "Software Version 7") {
version = H3C_V7
return
}
if strings.Contains(sysDesc, "S5500-SI") {
version = H3C_S5500
return
}
if strings.Contains(sysDesc, "Version S9500") {
version = H3C_S9500
return
}
if strings.Contains(sysDesc, "Version 3.1") {
version = H3C_V3_1
return
}
if strings.Contains(sysDesc, "Version ER") {
version = H3C_ER
return
}
if strings.Contains(sysDesc, "S5024P") {
version = H3C_S5024P
return
}
if strings.Contains(sysDesc, "S2126T") {
version = H3C_S2126T
return
}
version = H3C
return
}
if strings.Contains(sysDescLower, "futurematrix") {
version = FutureMatrix
return
}
if strings.Contains(sysDescLower, "huawei") {
if strings.Contains(sysDesc, "Version 5.") {
if strings.Contains(sysDesc, "Version 5.170") {
version = Huawei_V5_170
return
}
if strings.Contains(sysDesc, "Version 5.150") {
version = Huawei_V5_150
return
}
if strings.Contains(sysDesc, "Version 5.130") {
version = Huawei_V5_130
return
}
if strings.Contains(sysDesc, "Version 5.70") {
version = Huawei_V5_70
return
}
version = Huawei_V5
return
}
if strings.Contains(sysDesc, "Version 3.10") {
version = Huawei_V3_10
return
}
version = Huawei
return
}
if strings.Contains(sysDescLower, "ruijie") {
if strings.Contains(sysDesc, "S57") {
version = Ruijie_S5700
return
}
if strings.Contains(sysDesc, "S19") {
version = Ruijie_S1900
return
}
version = Ruijie
return
}
if strings.Contains(sysDescLower, "juniper networks") {
version = Juniper
return
}
if strings.Contains(sysDescLower, "dell networking") {
version = Dell
return
}
if strings.Contains(sysDescLower, "draytek") {
version = Draytek
return
}
if strings.Contains(sysDescLower, "fortigate") {
version = FortiGate
return
}
if strings.Contains(sysDescLower, "linux") {
if strings.Contains(sysDescLower, "armv7l") {
version = Sundray
return
}
version = Linux
return
}
err = fmt.Errorf("unknown vendor")
return
}
func getVersionNumber(sysdescr string) string {
version_number := ""
s := strings.Fields(sysdescr)
for index, value := range s {
if strings.ToLower(value) == "version" {
version_number = s[index+1]
}
}
version_number = strings.Replace(version_number, "(", "", -1)
version_number = strings.Replace(version_number, ")", "", -1)
return version_number
}
func SysPatchInfo(ip, community string, retry int, timeout int) (patch string, err error) {
oid := "1.3.6.1.4.1.2011.5.25.19.1.8.5.1.1.4"
method := snmpBulkWalk
var snmpPDUs []gosnmp.SnmpPDU
snmpPDUs, err = RunSnmp(ip, community, oid, method, retry, timeout)
for i, pdu := range snmpPDUs {
if len(string(pdu.Value.([]byte))) > 0 {
patch = fmt.Sprintf("%s<br>slot%d patch: %s", patch, i, string(pdu.Value.([]byte)))
}
}
return
}
func RemoveDuplicateElement(s []string) []string {
result := make([]string, 0, len(s))
temp := map[string]struct{}{}
for _, item := range s {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}