-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnmp.py
88 lines (69 loc) · 2.56 KB
/
snmp.py
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
from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, UdpTransportTarget, ContextData, ObjectType, ObjectIdentity, OctetString
def retrieve(hostname, port, community, *names):
iterator = nextCmd(
SnmpEngine(),
CommunityData(community, mpModel=1),
UdpTransportTarget((hostname, port)),
ContextData(),
ObjectType(
ObjectIdentity(
*names,
)
.addAsn1MibSource(
'file://.',
'file:///usr/share/snmp',
)
),
lexicographicMode=False,
)
for response in iterator:
errorIndication, errorStatus, errorIndex, varBinds = response
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
yield varBind[0].getMibSymbol(), varBind[1]
def gather(host, port, community):
apStats = {}
macApAlias = {}
macSsidAlias = {}
assocSsidStats = {}
assocApStats = {}
auth = (host, port, community)
for oid, value in retrieve(*auth, 'AI-AP-MIB', 'aiAccessPointTable'):
_, label, index = oid
mac = index[0].prettyPrint()
if mac not in apStats:
apStats[mac] = {
'ssid': [],
'mac': [],
}
assocApStats[mac] = 0
apStats[mac][label] = value
for oid, value in retrieve(*auth, 'AI-AP-MIB', 'aiWlanEntry'):
_, label, index = oid
mac = index[0].prettyPrint()
idx = index[1]
if mac not in apStats.keys():
# AP appeared during stat retrieval
continue
if label == 'aiWlanESSID':
apStats[mac]['ssid'].insert(idx, value)
elif label == 'aiWlanMACAddress':
apStats[mac]['mac'].insert(idx, value)
macApAlias[value] = mac
for mac in apStats:
for idx, ssid in enumerate(apStats[mac]['ssid']):
macSsidAlias[apStats[mac]['mac'][idx]] = ssid
if ssid not in assocSsidStats:
assocSsidStats[ssid] = 0
del apStats[mac]['mac']
del apStats[mac]['ssid']
for oid, value in retrieve(*auth, 'AI-AP-MIB', 'aiClientWlanMACAddress'): # from aiClientTable
_, label, index = oid
assocSsidStats[macSsidAlias[value]] += 1
assocApStats[macApAlias[value]] += 1
return apStats, assocApStats, assocSsidStats