/
MultiAutoShark.py
339 lines (300 loc) · 11.7 KB
/
MultiAutoShark.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
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
#!/usr/bin/env python
import threading
import Queue
import time
from device import Device
import xmltodict
import urllib2
import time
queue = Queue.Queue()
queueError = Queue.Queue()
queueSuccess = Queue.Queue()
threadQueue = Queue.Queue()
outlock = threading.Lock()
class interface:
def __init__(self,intName):
self.interface=intName
self.state = ""
self.eth_outrate1_bits= 0
self.eth_outrate1_pkts= 0
self.eth_inucast= 0
self.eth_inmcast= 0
self.eth_inbcast= 0
self.eth_inpkts= 0
self.eth_inbytes= 0
self.eth_jumbo_inpkts= 0
self.eth_storm_supp= 0
self.eth_runts= 0
self.eth_giants= 0
self.eth_crc= 0
self.eth_nobuf= 0
self.eth_inerr= 0
self.eth_frame= 0
self.eth_overrun= 0
self.eth_underrun= 0
self.eth_ignored= 0
self.eth_watchdog= 0
self.eth_bad_eth= 0
self.eth_bad_proto= 0
self.eth_in_ifdown_drops= 0
self.eth_dribble= 0
self.eth_indiscard= 0
self.eth_inpause= 0
self.eth_outucast= 0
self.eth_outmcast= 0
self.eth_outbcast= 0
self.eth_outpkts= 0
self.eth_outbytes= 0
self.eth_jumbo_outpkts= 0
self.eth_outerr= 0
self.eth_coll= 0
self.eth_deferred= 0
self.eth_latecoll= 0
self.eth_lostcarrier= 0
self.eth_nocarrier= 0
self.eth_babbles= 0
self.eth_outdiscard= 0
self.eth_outpause= 0
def populateCounters(self,singleIntDic):
try:
self.state = singleIntDic["state"]
self.eth_outrate1_bits= int(singleIntDic["eth_outrate1_bits"])
self.eth_outrate1_pkts= int(singleIntDic["eth_outrate1_pkts"])
self.eth_inucast= int(singleIntDic["eth_inucast"])
self.eth_inmcast= int(singleIntDic["eth_inmcast"])
self.eth_inbcast= int(singleIntDic["eth_inbcast"])
self.eth_inpkts= int(singleIntDic["eth_inpkts"])
self.eth_inbytes= int(singleIntDic["eth_inbytes"])
self.eth_jumbo_inpkts= int(singleIntDic["eth_jumbo_inpkts"])
self.eth_storm_supp= int(singleIntDic["eth_storm_supp"])
self.eth_runts= int(singleIntDic["eth_runts"])
self.eth_giants= int(singleIntDic["eth_giants"])
self.eth_crc= int(singleIntDic["eth_crc"])
self.eth_nobuf= int(singleIntDic["eth_nobuf"])
self.eth_inerr= int(singleIntDic["eth_inerr"])
self.eth_frame= int(singleIntDic["eth_frame"])
self.eth_overrun= int(singleIntDic["eth_overrun"])
self.eth_underrun= int(singleIntDic["eth_underrun"])
self.eth_ignored= int(singleIntDic["eth_ignored"])
self.eth_watchdog= int(singleIntDic["eth_watchdog"])
self.eth_bad_eth= int(singleIntDic["eth_bad_eth"])
self.eth_bad_proto= int(singleIntDic["eth_bad_proto"])
self.eth_in_ifdown_drops= int(singleIntDic["eth_in_ifdown_drops"])
self.eth_dribble= int(singleIntDic["eth_dribble"])
self.eth_indiscard= int(singleIntDic["eth_indiscard"])
self.eth_inpause= int(singleIntDic["eth_inpause"])
self.eth_outucast= int(singleIntDic["eth_outucast"])
self.eth_outmcast= int(singleIntDic["eth_outmcast"])
self.eth_outbcast= int(singleIntDic["eth_outbcast"])
self.eth_outpkts= int(singleIntDic["eth_outpkts"])
self.eth_outbytes= int(singleIntDic["eth_outbytes"])
self.eth_jumbo_outpkts= int(singleIntDic["eth_jumbo_outpkts"])
self.eth_outerr= int(singleIntDic["eth_outerr"])
self.eth_coll= int(singleIntDic["eth_coll"])
self.eth_deferred= int(singleIntDic["eth_deferred"])
self.eth_latecoll= int(singleIntDic["eth_latecoll"])
self.eth_lostcarrier= int(singleIntDic["eth_lostcarrier"])
self.eth_nocarrier= int(singleIntDic["eth_nocarrier"])
self.eth_babbles= int(singleIntDic["eth_babbles"])
self.eth_outdiscard= int(singleIntDic["eth_outdiscard"])
self.eth_outpause= int(singleIntDic["eth_outpause"])
except Exception as e:
#print "\t\t\tError storing info in exceptions:",e, self.interface
pass
class root_Device:
def __init__(self,hostname,proccesor_ID,version,connectedOn,user,pw):
self.proccesor_ID = proccesor_ID
self.connectedOn = connectedOn
self.username = user
self.password = pw
self.hostname = hostname
self.version = version
self.ipIntBrief = []
self.vrfList = []
self.intStatus = []
self.portChannels = []
self.neighbors = []
self.interfaceCounters = None
def addNeighbor(self,neighbor):
self.neighbors.append(neighbor)
def addIp(self,ip):
self.ipIntBrief.append(ip)
def addIntStatus(self,interface):
self.intStatus.append(interface)
def addVrf(self,vrf):
self.vrfList.append(vrf)
def addPortChannel(self,channel):
self.portChannels.append(channel)
def updateInterfaceCounters(self,intObj):
self.interfaceCounters = intObj
class Neighbors:
def __init__(self,NeighborRootDevice,NeighborDict):
self.rootDevice = NeighborRootDevice
self.NeighborDict = NeighborDict
class Vrf:
def __init__(self,name):
self.prefixList = []
self.name = name
def addPrefix(self,prefix):
self.prefixList.append(prefix)
def get_cdp_info(sw1):
sh_neigh = sw1.show('show cdp neighbor detail')
sh_neigh_dict = xmltodict.parse(sh_neigh[1])
neighbors = sh_neigh_dict['ins_api']['outputs']['output']['body']["TABLE_cdp_neighbor_detail_info"]['ROW_cdp_neighbor_detail_info']
return neighbors
def get_portchannel_sum(sw1):
sh_chan = sw1.show('show port-channel summary')
sh_chan_dict = xmltodict.parse(sh_chan[1])
if sh_chan_dict['ins_api']['outputs']['output']['body'] != None:
portChannels = sh_chan_dict['ins_api']['outputs']['output']['body']["TABLE_channel"]['ROW_channel']
return portChannels
def get_hostname_serial_version(sw1):
sh_ver= sw1.show('show version')
sh_ver_dict = xmltodict.parse(sh_ver[1])
proccesor_ID = sh_ver_dict['ins_api']['outputs']['output']['body']["proc_board_id"]
hostname = sh_ver_dict['ins_api']['outputs']['output']['body']["host_name"]
version = sh_ver_dict['ins_api']['outputs']['output']['body']["kickstart_ver_str"]
return hostname,proccesor_ID,version
def get_ip_int_b(sw1):
sh_ip_b = sw1.show('show ip int brief')
sh_ip_b_dict = xmltodict.parse(sh_ip_b[1])
int_brief = sh_ip_b_dict['ins_api']['outputs']['output']['body']["TABLE_intf"]
return int_brief
def get_vrf(sw1):
sh_ip_vrf = sw1.show("sh ip route vrf all")
sh_ip_vrf_dict = xmltodict.parse(sh_ip_vrf[1])
sh_vrf = sh_ip_vrf_dict['ins_api']['outputs']['output']['body']["TABLE_vrf"]["ROW_vrf"]
return sh_vrf
def get_int_status(sw1):
sh_status = sw1.show('show interface status')
sh_status_dict = xmltodict.parse(sh_status[1])
int_status = sh_status_dict['ins_api']['outputs']['output']['body']["TABLE_interface"]["ROW_interface"]
return int_status
def get_int(sw1):
sh_int = sw1.show('show interface')
sh_int_dict = xmltodict.parse(sh_int[1])
interfaces = sh_int_dict['ins_api']['outputs']['output']['body']["TABLE_interface"]["ROW_interface"]
return interfaces
def crawlDevice(ip_address,user,pw):
sw1 = Device(ip=ip_address, username=user, password=pw)
sw1.open()
# Getting everything into dicts
sh_vrf = get_vrf(sw1)
int_brief = get_ip_int_b(sw1)
int_status = get_int_status(sw1)
hostname,proccesor_ID,version = get_hostname_serial_version(sw1)
neighbors = get_cdp_info(sw1)
port_channels = get_portchannel_sum(sw1)
# Adding all data into objs
LocalDevice = root_Device(hostname,proccesor_ID,version,ip_address,user,pw)
for singleVrf in sh_vrf:
vrf = Vrf(singleVrf["vrf-name-out"])
if "TABLE_prefix" in singleVrf["TABLE_addrf"][ "ROW_addrf"].keys():
for prefixes in singleVrf["TABLE_addrf"][ "ROW_addrf"]["TABLE_prefix"]["ROW_prefix"]:
vrf.addPrefix(prefixes["ipprefix"])
LocalDevice.addVrf(vrf)
for ipInter in int_brief:
LocalDevice.addIp(ipInter["ROW_intf"]["prefix"])
LocalDevice.addPortChannel(port_channels)
for interface in int_status:
LocalDevice.addIp(interface)
for neighbor in neighbors:
neighEntry = Neighbors(root_Device,neighbor)
LocalDevice.addNeighbor(neighEntry)
return LocalDevice
def info_crawl(seedIp,user,password):
discoveredObjs =[]
crawledSerials = []
uncrawled=[seedIp]
crawledIps =[]
for ipN in uncrawled:
try:
if ipN not in crawledIps:
newD = crawlDevice(ipN, user, password)
crawledIps.append(ipN)
print "Connection established to:", ipN
if newD.proccesor_ID not in crawledSerials:
crawledSerials.append(newD.proccesor_ID)
discoveredObjs.append(newD)
for neigh in newD.neighbors:
neighborIp = (str(neigh.NeighborDict["v4mgmtaddr"]))
if neighborIp not in uncrawled:
uncrawled.append(neighborIp)
print "\t",neigh.NeighborDict["v4mgmtaddr"], "DISCOVERED"
except urllib2.HTTPError:
print "[!]Cannont establish to:",ipN
except Exception as e:
print "[!]ERROR: ",e
print "--------CDP Crawl Complete--------\n"
return discoveredObjs
def mon_device(device):
threadQueue.put(1)
deviceIp = device.connectedOn
username = device.username
passwor = device.password
sw1 = Device(ip=deviceIp, username=username, password=passwor)
sw1.open()
flag = True
print "Monitoring:",deviceIp
while flag:
allPack = []
interfacers = get_int(sw1)
device.updateInterfaceCounters(interfacers)
for i in interfacers:
x = interface(i["interface"])
x.populateCounters(i)
#if x.eth_giants > 0 or x.eth_jumbo_inpkts > 0 or x.eth_crc > 0 or x.eth_giants or x.eth_inerr:
if x.eth_outpkts > 1000:
print "Over 1000 on ", x.interface, x.eth_outpkts,deviceIp
conf = sw1.conf("config t ; ip access-list MonACLTemp ; permit ip any any log ;" + "int " + x.interface + " ; ip access-group MonACLTemp in ; end ; ethanalyzer local interface inband write bootflash:test.cap ")
print "\tBeginning Cap for 1 min",deviceIp
time.sleep(10)
print "\tDone capturing, now Cleaning config",deviceIp
clean = sw1.conf("config t ;" + " int " + x.interface + " ; no ip access-group MonACLTemp in ; exit ; no ip access-list MonACLTemp ; end")
clearing_counters = sw1.conf("clear counters interface" + x.interface)
# Remove flag for continous moitoring, with flag on it stops after the interface is noticed
flag = False
else:
allPack.append(x.eth_outpkts)
# if x.state == "up" and "Ether" in x.interface:
# print x.interface, x.state, x.eth_outpkts
time.sleep(3)
print max(allPack), "highest packet count number under 1000 on",deviceIp
queue.put("RANDOM DATA FOR QUEUE")
threadQueue.get(False, 2)
print "Done Cleaning, capture at bootflash:test.cap", deviceIp
def threadWrapper(deviceList,threadNumber):
time1 = time.time()
# Holds all the threads that will be created
threads = []
for device in deviceList:
# Limits threads to threadNumber, if you go over with queue size, it will sleep for 10 seconds then check again.
while threadQueue.qsize() > threadNumber: #thread limiter loop
#Sleeps
time.sleep(10)
# Target is your function to run in parrel, and args are the arguments for the function
t = threading.Thread( target=mon_device,args=(device,) )
t.start()
threads.append(t)
# Rejoin the threads
for t in threads:
t.join()
# Empty queue into whatever, probably a file
while not queue.empty():
# Print function replace in not a template
x1 = queue.get()
print "DONE"
time2 = time.time()
print "Time to run complete request: " + str(time2 - time1) ### make point to gui
def main():
threadNumber = 50
seed = "172.31.217.136"
username = "admin"
password = "cisco123"
devicesList = info_crawl(seed,username,password)
threadwrapper = threadWrapper(devicesList,threadNumber)
print "DONE FINAL!!!!"
if __name__ == '__main__':
main()
#" monitor session 30 type erspan-source ; description ERSPAN to 30 to POC ; source interface " + sourceInt + " both ; destination ip " + ip + " ; erspan-id 30 ; ip ttl 64 ; ip prec 0 ; ip dscp 0 ; mtu 1500 ; header-type 2 ; no shut ;"
#"monitor session 30 type erspan-destination ; description ERSPAN to 30 to POC ; erspan-id 30 ; vrf " + VVRFNAME + " ; source ip " + IP + " ; destination " + DES + " ; no shut ;"