-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathap-client-list-aruba-easysnmp.py3
executable file
·293 lines (214 loc) · 6.46 KB
/
ap-client-list-aruba-easysnmp.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
#!/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 easysnmp import Session, EasySNMPConnectionError, EasySNMPTimeoutError
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 = "{:02x}".format(ord(bytes[0]), "x")
b1 = "{:02x}".format(ord(bytes[1]), "x")
b2 = "{:02x}".format(ord(bytes[2]), "x")
b3 = "{:02x}".format(ord(bytes[3]), "x")
b4 = "{:02x}".format(ord(bytes[4]), "x")
b5 = "{:02x}".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
try:
session = Session(hostname=vc, community=snmpcomm, version=2)
# Connection timed out in some fashion
except EasySNMPConnectionError:
print('VC', vc, 'seems to be down, skipping...')
continue
results = []
apmacs = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.1.1.1'
try:
results = session.walk(oid)
except EasySNMPTimeoutError:
print('Bad SNMP community for VC', vc, '? Skipping...')
print()
continue
for apmac in results:
apmac = smac2mac(apmac.value)
apmacs.append(apmac)
# Station list
results = []
clients = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.1'
results = session.walk(oid)
for mac in results:
mac = mac.value
coid = smac2oid(mac)
# client to BSSID association
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.2' + '.' + coid
bssid = smac2mac((session.get(oid).value))
# Client uptimes
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.16' + '.' + coid
cuptime = (session.get(oid).value)
# SNRs
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.4.1.7' + '.' + coid
snr = (session.get(oid).value)
clients.append([smac2mac(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
ap = (session.get(oid).value)
# Uptime
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.1.1.9' + '.' + apmacdec
ticks = session.get(oid).value
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
results = []
channels = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.2.1.4' + '.' + apmacdec
results = session.walk(oid)
for channel in results:
channel = channel.value
channels.append(channel)
# BSSIDs on this AP
results = []
apbssids = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.3.1.4' + '.' + apmacdec
results = session.walk(oid)
for apbssid in results:
apbssid = smac2mac(apbssid.value)
apbssids.append(apbssid)
# ESSIDs on this AP
results = []
essids = []
oid = '1.3.6.1.4.1.14823.2.3.3.1.2.3.1.3' + '.' + apmacdec
results = session.walk(oid)
for essid in results:
essid = essid.value
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]):
mac = row[0]
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
# 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 ()