-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathap-client-list-aruba-pysnmp4.py3
executable file
·378 lines (297 loc) · 9.63 KB
/
ap-client-list-aruba-pysnmp4.py3
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python3
#
# Tested on IAP Virtual Cluster running 8.6.x.x, 8.10.x.x
#
#================================================================
import argparse
import getpass
import time
import datetime
from colorama import Style, Fore, Back
from pysnmp.hlapi import *
parser = argparse.ArgumentParser(description = 'Connects to Aruba IAP VC(s), enumerates APs, and connected client info. SNMP community will be read via prompt.')
parser.add_argument('-vcs', type=str, required=True, help='Comma seperated list of VCs')
parser.add_argument('-m', type=str, metavar='MACFILE', default='/usr/local/etc/internal-macs.txt', help='Location of MAC address file (optional), defaults to /usr/local/etc/internal-macs.txt. Format is simply mac <tab> short_hostname. MAC format is xx:xx:xx:xx:xx:xx')
args = parser.parse_args()
vclist = args.vcs.split(",")
snmpcomm = getpass.getpass(prompt='SNMP community: ')
macfile = args.m
# Read in internal mac file
hostdb = {}
try:
with open(macfile) as macs:
for line in macs:
(key, val) = line.split()
hostdb[key] = val
except EnvironmentError:
print ('Problem reading file', macfile, "!")
exit(1)
# Convert xx:xx:xx:xx:xx:xx to OID
def mac2oid(mac):
bytes = mac.split(':')
b0 = int(bytes[0], 16)
b1 = int(bytes[1], 16)
b2 = int(bytes[2], 16)
b3 = int(bytes[3], 16)
b4 = int(bytes[4], 16)
b5 = int(bytes[5], 16)
b0=str(b0)
b1=str(b1)
b2=str(b2)
b3=str(b3)
b4=str(b4)
b5=str(b5)
macdec = b0 + '.' + b1 + '.' + b2 + '.' + b3 + '.' + b4 + '.' + b5
return macdec
# Convert ASCII MAC to OID
def smac2oid(mac):
bytes = []
for char in mac:
bytes.append(char)
b0 = (ord(bytes[0]))
b1 = (ord(bytes[1]))
b2 = (ord(bytes[2]))
b3 = (ord(bytes[3]))
b4 = (ord(bytes[4]))
b5 = (ord(bytes[5]))
b0=str(b0)
b1=str(b1)
b2=str(b2)
b3=str(b3)
b4=str(b4)
b5=str(b5)
macdec = b0 + '.' + b1 + '.' + b2 + '.' + b3 + '.' + b4 + '.' + b5
return macdec
# Convert ASCII MAC to xx:xx:xx:xx:xx:xx MAC
def smac2mac(mac):
bytes = []
for char in mac:
bytes.append(char)
b0 = format(ord(bytes[0]), "x")
b1 = format(ord(bytes[1]), "x")
b2 = format(ord(bytes[2]), "x")
b3 = format(ord(bytes[3]), "x")
b4 = format(ord(bytes[4]), "x")
b5 = format(ord(bytes[5]), "x")
b0=str(b0)
b1=str(b1)
b2=str(b2)
b3=str(b3)
b4=str(b4)
b5=str(b5)
mac = b0 + ':' + b1 + ':' + b2 + ':' + b3 + ':' + b4 + ':' + b5
return mac
# Start with a blank line
print ()
# iterate vcs
for vc in vclist:
print ('Connecting to VC:', vc)
print ()
print ('Gathering data...')
print ()
# Get mac addresses of ap's from vc
apmacs = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.1.1.1'
for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
# If this fails already, we will consider the host down
if errorIndication:
print(errorIndication)
print('VC', vc, 'seems to be down, skipping...')
continue
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
print('VC', vc, 'seems to be down, skipping...')
continue
else:
for varBind in varBinds:
apmac = varBind[1].prettyPrint()
apmac = apmac[2:]
apmac = ':'.join(apmac[i:i+2] for i in range(0, len(apmac), 2))
apmacs.append(apmac)
# Station list
clients = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.1'
macbug = 0
for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
mac = varBind[1].prettyPrint()
# Got a string encoded mac returned
if (len(mac) == 6):
coid = smac2oid(mac)
else:
mac = mac[2:]
mac = ':'.join(mac[i:i+2] for i in range(0, len(mac), 2))
coid = mac2oid(mac)
# client to BSSID association
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.2' + '.' + coid
for (errorIndication, errorStatus, errorIndex, varBinds) in getCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
bssid = varBind[1]
# Client uptimes
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.16' + '.' + coid
for (errorIndication, errorStatus, errorIndex, varBinds) in getCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
cuptime = varBind[1]
# SNRs
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.7' + '.' + coid
for (errorIndication, errorStatus, errorIndex, varBinds) in getCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
snr = varBind[1]
clients.append([mac,bssid,cuptime,snr])
# Uncomment to short circuit
#continue
for apmac in apmacs:
apmacdec = mac2oid(apmac)
ticks = 0
# AP name
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.1.1.2' + '.' + apmacdec
for (errorIndication, errorStatus, errorIndex, varBinds) in getCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
ap = varBind[1]
ap = str(ap)
# Uptime
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.1.1.9' + '.' + apmacdec
oid = str(oid)
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)))
)
if not errorIndication and not errorStatus:
for varBind in varBinds:
ticks = varBinds[0][1]
ticks = int(ticks)
upseconds = ticks/100
uptime = datetime.timedelta(seconds=upseconds)
# Strip microseconds
uptime = str(uptime).split(".")[0]
# Print current host, uptime
print ('%-20s Uptime: %s' % (ap, uptime))
print ("=============================================================================")
# Radio channels
channels = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.2.1.4' + '.' + apmacdec
for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
channel = varBind[1]
channels.append(channel)
# BSSIDs on this AP
apbssids = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.3.1.4' + '.' + apmacdec
for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
apbssid = varBind[1]
apbssids.append(apbssid)
# ESSIDs on this AP
essids = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.3.1.3' + '.' + apmacdec
for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
CommunityData(snmpcomm),
UdpTransportTarget((vc, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)), lexicographicMode=False
):
if not errorIndication and not errorStatus:
for varBind in varBinds:
essid = varBind[1]
essids.append(essid)
# Print channels, more header misc.
chanstr = ""
for i in channels:
chanstr = (chanstr + " " + i)
print ("Current frequencies: " + Fore.CYAN + "Channels:%s" % (chanstr), end = '')
print (Style.RESET_ALL)
print ("-----------------------------------------------------------------------------")
print ("Client MAC SSID SNR Uptime")
print ("-----------------------------------------------------------------------------")
index = 0
ssidindex = 0
for apbssid in apbssids:
ssid = essids[ssidindex]
cssid = Fore.CYAN + ssid + Style.RESET_ALL
ssidindex += 1
for row in clients:
if (apbssid == row[1]):
arubabug = 0
mac = row[0]
# Sometimes we get client macs as ASCII strings. Aruba says this is normal. Agree to disagree...
if (len(mac) == 6):
mac = smac2mac(mac)
arubabug = 1
cuptime = row[2]
cuptime = int(cuptime)
cupseconds = cuptime/100
cuptime = datetime.timedelta(seconds=cupseconds)
# Strip microseconds
cuptime = str(cuptime).split(".")[0]
snr = row[3]
cmac = Fore.YELLOW + mac + Style.RESET_ALL
snr = str(snr).zfill(2)
csnr = Fore.BLUE + snr + Style.RESET_ALL
if mac in hostdb.keys():
client = hostdb[mac]
cclient = Fore.MAGENTA + client + Style.RESET_ALL
else:
client = "UNKNOWN"
cclient = Fore.RED + client + Style.RESET_ALL
if (arubabug == 1):
cmac = cmac + "*"
# Print out each entry
print ('%-32s %-28s %-20s %-6s %18s' % (cclient, cmac, cssid, csnr, cuptime))
index += 1
# No clients connected...
if (index == 0):
print ("NO CLIENTS")
print ()
# Blank seperator line
#print ()