-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnmpwalk_get_ifnum.py
More file actions
executable file
·110 lines (78 loc) · 2.7 KB
/
snmpwalk_get_ifnum.py
File metadata and controls
executable file
·110 lines (78 loc) · 2.7 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
このプログラムはhostnameで指定されているホストから、
IF-MIB::ifDescrのTreeを表示します
"""
# 変数
comm = "public"
hostname = "192.168.101.112"
# cmdgen を importする
# pip install pysnmpが必要
# pip install pysnmp-mibs
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi import builder, view, error
import sys
def mac2dec(x):
# "01:2:3:4:5:ff"
# [1, 2, 3, 4, 5, 255]
return [int(x, 16) for x in x.split(":")]
mac = "00:0c:29:79:2f:88"
# IF-MIB::ifDescr
mib_ifDescr = ".1.3.6.1.2.1.2.2.1.2"
# BRIDGE-MIB::dot1dTpFdbAddress.
mibid_bridge=".1.3.6.1.2.1.17.4.3.1.1"
# Q-BRIDGE-MIB::dot1qTpFdbPort
mibid_qbridge=".1.3.6.1.2.1.17.7.1.2.2.1.2.7"
# ターゲットのMIB Treeをtupleにする。
# ".1.3.6.1.2.1.2.2.1.2" -> [1, 3, 6, 1, 2, 1, 2, 2, 1, 2]
mibstr2tuple = lambda x: [int(x) for x in mib_ifDescr.split(".")[1:]]
mibtuple = mibstr2tuple(mib_ifDescr)
t = mibid_bridge + "." + ".".join(map(lambda x: str(x), mac2dec(mac)))
mibstr2tuple = lambda x: [int(x) for x in t.split(".")[1:]]
mibtuple = mibstr2tuple(mib_ifDescr)
t = mibid_qbridge + "." + ".".join(map(lambda x: str(x), mac2dec(mac)))
mibstr2tuple = lambda x: [int(x) for x in t.split(".")[1:]]
mibtuple = mibstr2tuple(mib_ifDescr)
print(t)
# 操作用クラスの生成
cmdGen = cmdgen.CommandGenerator()
comm_data = cmdgen.CommunityData(comm, comm)
# 接続先ホスト名とポートの指定
transport = cmdgen.UdpTransportTarget((hostname, int(161)))
errorIndication, errorStatus, errorIndex, result = cmdgen.CommandGenerator().nextCmd(comm_data, transport, mibtuple)
# resultには結果が入っている
# 例: print result[0]
# [(ObjectName(1.3.6.1.2.1.2.2.1.2.1), OctetString('10GigabitEthernet1/1'))]
# errorIndication: None = 異常なし
if errorIndication:
print(errorIndication)
sys.exit(1)
# errorStatus: 0 = 正常
if errorStatus:
print('%s at %s\n' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
))
sys.exit(1)
# 出力を表示
for varBindTableRow in result:
for name, val in varBindTableRow:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
###
"""
path_to_mib_dir = "/usr/share/mibs/ietf/SNMPv2-SMI"
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibPath() + ( path_to_mib_dir, )
mibBuilder.setMibPath( *mibPath )
mibBuilder.loadModules(
builder.ZipMibSource('SNMPv2-SMI'),
)
"""
"""
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibSources() + (builder.DirMibSource('/usr/share/mibs/'),)
mibBuilder.setMibSources(*mibPath)
mibBuilder.loadModules('SNMPv2-MIB')
mibView = view.MibViewController(mibBuilder)
"""