def mon_device(device,seconds,tabs,destinationIp): threadQueue.put(1) destIp = destinationIp tabs = tabs*2 deviceIp = device.connectedOn username = device.username passwor = device.password sw1 = Device(ip=deviceIp, username=username, password=passwor) sw1.open() flag = True print "Monitoring:",deviceIp try: 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 ("-" * tabs)+ "Over 1000 on ", x.interface, x.eth_outpkts,deviceIp create_span = sw1.conf(" conf t ; monitor session 30 type erspan-source ; description ERSPAN30 for TEST ; source interface " + x.interface + " both ; destination ip " + destIp + " ; vrf default ; erspan-id 30 ; ip ttl 64 ; ip prec 0 ; ip dscp 0 ; mtu 1500 ; header-type 2 ; no shut ; end ;") print ("-" * tabs) + "Beginning erspan for", seconds ,"seconds","to",deviceIp,"on int",x.interface time.sleep(10) print ("-" * tabs) + "Done capturing, now Cleaning config",deviceIp,"on int",x.interface clean = sw1.conf("config t ;" + " no monitor session 30 ; end") clearing_counters = sw1.conf("clear counters interface" + x.interface) print ("-" * tabs) + "Done Cleaning, erspan sent to", destIp, "from",deviceIp, "-", 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,x.interface]) # if x.state == "up" and "Ether" in x.interface: # print x.interface, x.state, x.eth_outpkts time.sleep(3) ints = [] interf = [] for i in allPack: ints.append(i[0]) interf.append(i[1]) maxInt = max(ints) maxInterface = interf[ints.index(maxInt)] print ("-" * tabs) + "Highest packet count is", maxInt, "that is under 1000 on",maxInterface except (KeyboardInterrupt, SystemExit): print ("-" * tabs) + "Keyboard Interrupt, Forcing cleaning of",deviceIp clean = sw1.conf("config t ;" + " no monitor session 30 ; end") print ("-" * tabs) + "Done Cleaning, erspan sent to", destIp, "from",deviceIp, queue.put("RANDOM DATA FOR QUEUE") threadQueue.get(False, 2)
def conf_intfs(conf_dict): ''' This connects to the chosen switch and gets all of the ports. and vlans. This is filtered to access ports only. params: conf_dict (dictionary), example: { "switch_ip": "172.31.217.135", "intf_desc": "Configured by NXAPI", "intf_id": [ "Ethernet1/1", "Ethernet1/2" ], "vlan_id": "31" } ''' config_changes_list = "" switch_user = '******' switch_pw = 'cisco123' switch_ip = conf_dict['switch_ip'] switch_name = get_switchname(switch_ip) # Connect to switch switch = Device(ip=switch_ip, username=switch_user, password=switch_pw) switch.open() # Parse data in to commands for item in range(len(conf_dict['intf_id'])): change_vlan = 'config t ; interface ' + conf_dict['intf_id'][ item] + ' ; switchport access vlan ' + conf_dict['vlan_id'] change_desc = 'config t ; interface ' + conf_dict['intf_id'][ item] + ' ; description ' + conf_dict['intf_desc'] # Send switch commands to NXAPI switch.conf(change_vlan) switch.conf(change_desc) # Generate NXOS-friendly config to return to consumer config_changes_list += show_run(switch_ip, conf_dict['intf_id'][item]) # Log cli_conf call to local file logtime = get_time() log_change(logtime + ': ' + switch_name + '(' + switch_user + '): ' + change_vlan + '\n') log_change(logtime + ': ' + switch_name + '(' + switch_user + '): ' + change_desc + '\n') return config_changes_list
def conf_intfs(conf_dict): """ This connects to the chosen switch and gets all of the ports. and vlans. This is filtered to access ports only. params: conf_dict (dictionary), example: { "switch_ip": "172.31.217.135", "intf_desc": "Configured by NXAPI", "intf_id": [ "Ethernet1/1", "Ethernet1/2" ], "vlan_id": "31" } """ config_changes_list = "" switch_user = "******" switch_pw = "cisco123" switch_ip = conf_dict["switch_ip"] switch_name = get_switchname(switch_ip) # Connect to switch switch = Device(ip=switch_ip, username=switch_user, password=switch_pw) switch.open() # Parse data in to commands for item in range(len(conf_dict["intf_id"])): change_vlan = ( "config t ; interface " + conf_dict["intf_id"][item] + " ; switchport access vlan " + conf_dict["vlan_id"] ) change_desc = "config t ; interface " + conf_dict["intf_id"][item] + " ; description " + conf_dict["intf_desc"] # Send switch commands to NXAPI switch.conf(change_vlan) switch.conf(change_desc) # Generate NXOS-friendly config to return to consumer config_changes_list += show_run(switch_ip, conf_dict["intf_id"][item]) # Log cli_conf call to local file logtime = get_time() log_change(logtime + ": " + switch_name + "(" + switch_user + "): " + change_vlan + "\n") log_change(logtime + ": " + switch_name + "(" + switch_user + "): " + change_desc + "\n") return config_changes_list
def conf_intfs(conf_dict): ''' This connects to the chosen switch and gets all of the ports. and vlans. This is filtered to access ports only. ''' config_changes_list = "" switch_user = '******' switch_pw = 'cisco123' switch_ip = conf_dict['switch_ip'] switch = Device(ip=switch_ip, username=switch_user, password=switch_pw) switch.open() for item in range(len(conf_dict['intf_id'])): change_vlan = 'config t ; interface ' + conf_dict['intf_id'][item] + ' ; switchport access vlan ' + conf_dict['vlan_id'] change_desc = 'config t ; interface ' + conf_dict['intf_id'][item] + ' ; description ' + conf_dict['intf_desc'] switch.conf(change_vlan) switch.conf(change_desc) config_changes_list += show_run(switch_ip, conf_dict['intf_id'][item]) return config_changes_list
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
ip = result['ins_api']['outputs']['output']['body']['TABLE_interface']['ROW_interface']['eth_ip_addr'] print ip mask = result['ins_api']['outputs']['output']['body']['TABLE_interface']['ROW_interface']['eth_ip_mask'] print mask print ip + '/' + mask sh_vlan = sw1.show('sh vl') sh_vlan_dict = xmltodict.parse(sh_vlan[1]) print json.dumps( sh_vlan_dict, indent=4) vlan10_name = sh_vlan_dict['ins_api']['outputs']['output']['body']['TABLE_vlanbrief']['ROW_vlanbrief'][1]['vlanshowbr-vlanname'] print vlan10_name vlans = sh_vlan_dict['ins_api']['outputs']['output']['body']['TABLE_vlanbrief']['ROW_vlanbrief'] for each in vlans: print 'VLAN ID: ', each['vlanshowbr-vlanid-utf'] print 'VLAN NAME: ', each['vlanshowbr-vlanname'] print '=' * 25 push_l3port = sw1.conf('config t ; interface e1/24 ; no switchport ') push_l3ip = sw1.conf('config t ; interface e1/24 ; ip address 172.19.21.1/24 ') cmd = 'config t ; int e1/24 ; '
print json.dumps(result, indent=4) ip = result['ins_api']['outputs']['output']['body']['TABLE_interface'][ 'ROW_interface']['eth_ip_addr'] print ip mask = result['ins_api']['outputs']['output']['body']['TABLE_interface'][ 'ROW_interface']['eth_ip_mask'] print mask print ip + '/' + mask sh_vlan = sw1.show('sh vl') sh_vlan_dict = xmltodict.parse(sh_vlan[1]) print json.dumps(sh_vlan_dict, indent=4) vlan10_name = sh_vlan_dict['ins_api']['outputs']['output']['body'][ 'TABLE_vlanbrief']['ROW_vlanbrief'][1]['vlanshowbr-vlanname'] print vlan10_name vlans = sh_vlan_dict['ins_api']['outputs']['output']['body'][ 'TABLE_vlanbrief']['ROW_vlanbrief'] for each in vlans: print 'VLAN ID: ', each['vlanshowbr-vlanid-utf'] print 'VLAN NAME: ', each['vlanshowbr-vlanname'] print '=' * 25 push_l3port = sw1.conf('config t ; interface e1/24 ; no switchport ') push_l3ip = sw1.conf('config t ; interface e1/24 ; ip address 172.19.21.1/24 ') cmd = 'config t ; int e1/24 ; '
def mon_device(device, seconds, tabs, destinationIp): threadQueue.put(1) destIp = destinationIp tabs = tabs * 2 deviceIp = device.connectedOn username = device.username passwor = device.password sw1 = Device(ip=deviceIp, username=username, password=passwor) sw1.open() flag = True print "Monitoring:", deviceIp try: 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( "-" * tabs ) + "Over 1000 on ", x.interface, x.eth_outpkts, deviceIp create_span = sw1.conf( " conf t ; monitor session 30 type erspan-source ; description ERSPAN30 for TEST ; source interface " + x.interface + " both ; destination ip " + destIp + " ; vrf default ; erspan-id 30 ; ip ttl 64 ; ip prec 0 ; ip dscp 0 ; mtu 1500 ; header-type 2 ; no shut ; end ;" ) print( "-" * tabs ) + "Beginning erspan for", seconds, "seconds", "to", deviceIp, "on int", x.interface time.sleep(10) print( "-" * tabs ) + "Done capturing, now Cleaning config", deviceIp, "on int", x.interface clean = sw1.conf("config t ;" + " no monitor session 30 ; end") clearing_counters = sw1.conf("clear counters interface" + x.interface) print( "-" * tabs ) + "Done Cleaning, erspan sent to", destIp, "from", deviceIp, "-", 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, x.interface]) # if x.state == "up" and "Ether" in x.interface: # print x.interface, x.state, x.eth_outpkts time.sleep(3) ints = [] interf = [] for i in allPack: ints.append(i[0]) interf.append(i[1]) maxInt = max(ints) maxInterface = interf[ints.index(maxInt)] print( "-" * tabs ) + "Highest packet count is", maxInt, "that is under 1000 on", maxInterface except (KeyboardInterrupt, SystemExit): print("-" * tabs) + "Keyboard Interrupt, Forcing cleaning of", deviceIp clean = sw1.conf("config t ;" + " no monitor session 30 ; end") print("-" * tabs ) + "Done Cleaning, erspan sent to", destIp, "from", deviceIp, queue.put("RANDOM DATA FOR QUEUE") threadQueue.get(False, 2)