def load_vlan(switch, silent=True): current_vlans = Vlan.objects.filter(on_switch=switch) device = get_switch(switch) vlan_names = device.vlan_names() # vlans on switch vlans_on_switch = device.vlans() if not vlans_on_switch: return Vlan.objects.get_or_create(vlan=0, name='Trunk') for vlan in vlans_on_switch: vlan_obj, created = Vlan.objects.get_or_create(vlan=vlan) if created and not silent: print('Created vlan %d on switch %s' % (vlan, switch)) # vlan name if str(vlan) in vlan_names: vlan_name = vlan_names[str(vlan)] if vlan_name.find('VLAN') < 0: vlan_obj.name = vlan_name vlan_obj.save() vlan_obj.on_switch.add(switch) # vlans in database for vlan in current_vlans: if vlan.vlan not in vlans_on_switch: if not silent: print('Vlan %d not on switch %s' % (vlan.vlan, switch)) vlan.on_switch.remove(switch) if not vlan.on_switch.all(): if not silent: print('Vlan %d not on any switches' % vlan.vlan) vlan.delete() else: vlan.save()
def handle(self, *args, **options): for switch in self.handle_arguments(options): print(switch) try: device = get_switch(switch) print(switch_info.switch_info(device=device)) except SNMPError as e: print(switch, e) continue
def handle(self, *args, **options): if not options['switch'][0] == 'all': switches = Switch.objects.filter(name=options['switch'][0]) else: switches = Switch.objects.all() for switch in switches: print(switch) try: device = get_switch(switch) print(switch_info.switch_info(device=device)) except EasySNMPTimeoutError: print('Timeout connecting to %s' % switch) continue
def handle(self, *args, **options): switch = Switch.objects.get(name=options['switch'][0]) device = get_switch(switch) arp = device.arp() # pprint(arp) # return for mac, ip in arp.items(): if not len(mac) == 12: mac = utils.mac_string(mac) try: arp_db = Arp(mac=mac, ip=ip) arp_db.save() except DataError as error: print(error) print(mac) print(utils.mac_string(mac))
def handle(self, *args, **options): switch: Switch for switch in self.handle_arguments(options): device = get_switch(switch) try: arp = device.arp() except SNMPError as e: print(e) continue for mac, ip in arp.items(): if not len(mac) == 12: mac = utils.mac_string(mac) try: arp_db = Arp(mac=mac, ip=ip) arp_db.save() except DataError as error: print(error) print(mac) print(utils.mac_string(mac))
def handle(self, *args, **cmd_options): print(cmd_options) switch = Switch.objects.get(ip=cmd_options['switch'][0]) print(switch) if not switch: print('No switches found') return options = backup_options(switch) if options is None: return cli_class = get_cli(switch.type) if not hasattr(cli_class, 'set_hostname'): print('Hostname editing on %s is not supported' % switch.type) exit() cli = connect_cli(switch) cli.set_hostname(cmd_options['hostname'][0]) device = get_switch(switch) print(switch_info.switch_info(device=device))
def load_interfaces(switch, now=None): if not now: now = datetime.now() device = get_switch(switch) interfaces = device.interfaces_rfc() try: interface_vlan, tagged_vlans, untagged_vlan = device.vlan_ports() except ValueError as exception: print(exception) print('Using pvid') interface_vlan = device.vlan_ports_pvid() tagged_vlans = None untagged_vlan = None uptime = device.uptime() if not interfaces: raise ValueError('No interfaces found on switch') ports_rev = dict() if switch.type == 'Cisco': vlans = Vlan.objects.filter(on_switch=switch, vlan__gt=0) if not vlans: raise ValueError('No vlans on switch, run load_vlans') ports = device.bridgePort_to_ifIndex() for vlan in vlans: try: ports_temp = device.bridgePort_to_ifIndex(vlan=vlan.vlan) if not ports_temp: print('No ports found in vlan %d' % vlan.vlan) continue for bridge_port, if_index in ports_temp.items(): ports[bridge_port] = if_index ports_rev[if_index] = bridge_port except EasySNMPTimeoutError: pass else: ports = device.bridgePort_to_ifIndex() for bridge_port, if_index in ports.items(): ports_rev[if_index] = bridge_port if not ports: raise ValueError('bridgePort to ifIndex conversion table not found') cdp_multi = device.cdp_multi() poe_status = device.interface_poe_status() if poe_status and not switch.has_poe: switch.has_poe = True switch.save() if switch.type == 'Aruba': stack = device.stack_ports() else: stack = [] # for bridge_port, if_index in ports.items(): for if_index in interfaces['type'].keys(): if if_index not in ports_rev: bridge_port = (str(int(if_index[-2:]))) else: bridge_port = ports_rev[if_index] if if_index in interfaces['name']: name = interfaces['name'][if_index] elif if_index in interfaces['descr']: name = switch.shorten_interface_name(interfaces['descr'][if_index]) else: name = '' # Interface naming for Westermo DSL modems if switch.type == 'Westermo': if interfaces['type'][if_index] == '6': name = '10/100TX Eth %s' % name elif interfaces['type'][if_index] == '169': name = 'SHDSL DSL %s' % name """ 117 is gigabitEthernet on HP 169 is DSL """ allowed_types = ['6', '117', '169'] if not interfaces['type'][if_index] in allowed_types: # print('Interface type %s' % interfaces['type'][if_index]) continue if name == 'Fa0': continue interface, new = Interface.objects.get_or_create( switch=switch, index=if_index, defaults={ 'interface': name, 'type': interfaces['type'][if_index] }) if not new: if not interface.status == int(interfaces['status'][if_index]): interface.link_status_changed = datetime.now() if if_index in interfaces['alias']: interface.description = interfaces['alias'][if_index] if if_index in interfaces['high_speed']: interface.speed = interfaces['high_speed'][if_index] else: interface.speed = None # print(if_index) # print(interfaces['status'][if_index]) interface.status = int(interfaces['status'][if_index]) interface.admin_status = int(interfaces['admin_status'][if_index]) interface.interface = name interface.type = interfaces['type'][if_index] if poe_status and bridge_port in poe_status: interface.poe_status = poe_status[bridge_port] else: interface.poe_status = None if switch.type == 'Westermo': neighbor = get_neighbors(int(ports_rev[if_index]), cdp_multi, switch) else: neighbor = get_neighbors(interface.index, cdp_multi, switch) if not neighbor and interface.neighbor: if interface.neighbor_set_by == switch: print( 'Clearing neigbor %s from %s, set by %s' % (interface.neighbor, interface, interface.neighbor_set_by)) interface.neighbor = None else: print( 'Keeping neigbor %s on %s set by %s' % (interface.neighbor, interface, interface.neighbor_set_by)) elif isinstance(neighbor, Switch): interface.neighbor = neighbor interface.neighbor_set_by = switch # Interfaces with CDP or LDDP is a link, # skip loading of MAC addresses interface.skip_mac = True else: interface.neighbor_string = neighbor # Mitel IP Phones have lldp, but we want to load their MAC if neighbor in ['Mitel IP Phone']: interface.force_mac = True if not switch.type == 'Cisco' and not switch.type == 'CiscoSB' and not switch.type == 'Aruba': key = int(bridge_port) else: key = int(if_index) if key not in interface_vlan or not interface_vlan[key]: # print('%d not in interface_vlan' % key) interface.vlan = None else: vlan = interface_vlan[key] try: interface.vlan = Vlan.objects.get(vlan=vlan) interface.vlan.has_ports = True interface.vlan.save() except Vlan.DoesNotExist: print('Missing vlan %s, run load_vlans' % vlan) if tagged_vlans and key in tagged_vlans: for tagged_vlan in tagged_vlans[key]: interface.tagged_vlans.add(Vlan.objects.get(vlan=tagged_vlan)) if untagged_vlan and key in untagged_vlan: try: interface.vlan = Vlan.objects.get(vlan=untagged_vlan[key]) interface.vlan.has_ports = True interface.vlan.save() except Vlan.DoesNotExist: print('Missing vlan %s, run load_vlans' % untagged_vlan[key]) if interface.index in stack: print('Interface %s in stack' % interface) interface.neighbor_string = stack[interface.index] else: print(interface.index) try: interface.save() except DataError as error: print(error) # if if_index in tagged_ports: # for vlan in tagged_ports[if_index]: # print('vlan %s is tagged on ifindex %s' % (vlan, if_index)) # # interface.tagged_vlans.add(vlan) del device.sessions[switch.ip]
def load_mac(switch: Switch, vlan=None): device = get_switch(switch) if not switch.type == 'Cisco': vlans = Vlan.objects.filter(vlan=0) else: vlans = Vlan.objects.filter(on_switch=switch, has_ports=True) if vlan: vlans.filter(vlan=vlan) if not vlans: print('No vlans on switch ' + switch.name) return now = datetime.now() mac_on_port = None for vlan in vlans: try: mac_on_port = device.mac_on_port(vlan=vlan.vlan, use_q_bridge_mib=switch.type == 'Comware') if not mac_on_port: print('No ports in vlan %s' % vlan) continue if switch.type == 'Cisco': # Cisco needs one query for each vlan bridge_port_to_ifindex = device.bridgePort_to_ifIndex(vlan=vlan.vlan) elif switch.type == 'Westermo' or switch.type == 'Comware': bridge_port_to_ifindex = device.bridgePort_to_ifIndex() else: bridge_port_to_ifindex = None except exceptions.SNMPTimeout: print('Timeout connecting to %s vlan %s' % (switch, vlan)) continue if not mac_on_port: print('No ports in vlan %s' % vlan) continue for mac, bridge_port in mac_on_port.items(): if not bridge_port_to_ifindex: if_index = bridge_port if switch.type == 'Extreme': if_index = str(int(if_index) + 1000) elif bridge_port not in bridge_port_to_ifindex: continue else: if_index = bridge_port_to_ifindex[bridge_port] try: interface_obj = Interface.objects.get(switch=switch, index=if_index) except Interface.DoesNotExist: # print('Interface with index %s not found' % if_index) continue # Do not load mac for trunk ports if (interface_obj.is_trunk() and not interface_obj.force_mac) or interface_obj.skip_mac: # print('mac: %s trunk interface: %s %s' % ( # mac, interface_obj.switch, interface_obj)) continue # print('mac: %s interface: %s' % (mac, interface)) try: mac, new = Mac.objects.get_or_create(mac=mac, defaults={'interface': interface_obj, 'last_seen': now}) if not new: if not mac.interface == interface_obj: print('MAC %s moved from %s to %s' % (mac, mac.interface.switch_string(), interface_obj.switch_string())) mac.interface = interface_obj mac.last_seen = now mac.save() except Mac.MultipleObjectsReturned: print('Multiple MAC') print(Mac.objects.filter(mac=mac_string(mac))) except exceptions.SNMPError as e: print(e) if mac_on_port: bad_macs = Mac.objects.filter(interface__switch=switch).exclude(last_seen=now) bad_macs.delete() device.sessions[switch.ip] = None
def load_interfaces(switch: Switch, now=None): if not now: now = datetime.now() device = get_switch(switch) interfaces = device.interfaces_rfc() try: if switch.type == 'Comware': interface_vlan, tagged_vlans, untagged_vlan = device.vlan_ports_static( ) else: interface_vlan, tagged_vlans, untagged_vlan = device.vlan_ports() except SNMPNoData as e: # HP 1910 if switch.type == 'Cisco': raise e interface_vlan = device.vlan_ports_pvid() tagged_vlans = None untagged_vlan = None uptime = device.uptime() ports_rev = dict() try: aggregations = device.aggregations() except SNMPError: aggregations = {} if switch.type == 'Cisco': try: ports = device.bridgePort_to_ifIndex() except SNMPError: ports = {} vlan_check = [] for vlan in untagged_vlan.values(): if vlan in vlan_check: continue vlan_check.append(vlan) try: try: ports_temp = device.bridgePort_to_ifIndex(vlan=vlan) except SNMPNoData: print('No ports found in vlan %d' % vlan) continue for bridge_port, if_index in ports_temp.items(): ports[bridge_port] = if_index ports_rev[if_index] = bridge_port device.close_sessions() except SNMPError: pass else: ports = device.bridgePort_to_ifIndex() for bridge_port, if_index in ports.items(): ports_rev[if_index] = bridge_port cdp_multi = device.cdp_multi() poe_status = device.interface_poe_status() if poe_status and not switch.has_poe: switch.has_poe = True switch.save() if switch.type == 'Aruba': try: stack = device.stack_ports() except SNMPNoData: stack = [] else: stack = [] # for bridge_port, if_index in ports.items(): for if_index in interfaces['type'].keys(): if if_index not in ports_rev: bridge_port = (str(int(if_index[-2:]))) else: bridge_port = ports_rev[if_index] if if_index in interfaces['name']: name = interfaces['name'][if_index] elif if_index in interfaces['descr']: name = switch.shorten_interface_name(interfaces['descr'][if_index]) else: name = '' # Interface naming for Westermo DSL modems if switch.type == 'Westermo': if interfaces['type'][if_index] == '6': name = '10/100TX Eth %s' % name elif interfaces['type'][if_index] == '169': name = 'SHDSL DSL %s' % name """ 117 is gigabitEthernet on HP 169 is DSL """ allowed_types = ['6', '117', '169', 'ethernetCsmacd', 'shdsl'] if not interfaces['type'][if_index] in allowed_types and \ int(if_index) not in aggregations.keys(): # print('Interface %s type %s' % (if_index, interfaces['type'][if_index])) continue if name == 'Fa0': continue interface, new = Interface.objects.get_or_create( switch=switch, index=if_index, defaults={ 'interface': name, 'type': interfaces['type'][if_index] }) if not new: if not interface.status == int(interfaces['status'][if_index]): interface.link_status_changed = datetime.now() if if_index in interfaces['alias']: interface.description = interfaces['alias'][if_index] if if_index in interfaces['high_speed']: interface.speed = interfaces['high_speed'][if_index] else: interface.speed = None # print(if_index) # print(interfaces['status'][if_index]) interface.status = int(interfaces['status'][if_index]) interface.admin_status = int(interfaces['admin_status'][if_index]) interface.interface = name interface.type = interfaces['type'][if_index] if poe_status and bridge_port in poe_status: interface.poe_status = poe_status[bridge_port] else: interface.poe_status = None if switch.type == 'Westermo': neighbor = get_neighbors(int(ports_rev[if_index]), cdp_multi, switch) elif switch.type == 'HP' or switch.type == 'Comware': neighbor = get_neighbors(int(bridge_port), cdp_multi, switch) else: neighbor = get_neighbors(interface.index, cdp_multi, switch) if not neighbor and interface.neighbor: if interface.neighbor_set_by == switch: print( 'Clearing neigbor %s from %s, set by %s' % (interface.neighbor, interface, interface.neighbor_set_by)) interface.neighbor = None else: print( 'Keeping neigbor %s on %s set by %s' % (interface.neighbor, interface, interface.neighbor_set_by)) elif isinstance(neighbor, Switch): interface.neighbor = neighbor interface.neighbor_set_by = switch # Interfaces with CDP or LDDP is a link, # skip loading of MAC addresses interface.skip_mac = True else: interface.neighbor_string = neighbor # Mitel IP Phones have lldp, but we want to load their MAC if neighbor in ['Mitel IP Phone']: interface.force_mac = True if switch.type not in ['Cisco', 'CiscoSB', 'Aruba']: key = int(bridge_port) else: key = int(if_index) if interface_vlan: if key not in interface_vlan or not interface_vlan[key]: if key not in interface_vlan: print('%d not in interface_vlan' % key) interface.vlan = None else: set_interface_vlan(interface, interface_vlan[key]) if tagged_vlans and key in tagged_vlans: for tagged_vlan in tagged_vlans[key]: set_interface_vlan(interface, tagged_vlan, True) if untagged_vlan and key in untagged_vlan: set_interface_vlan(interface, untagged_vlan[key]) if interface.index in stack: print('Interface %s in stack' % interface) interface.neighbor_string = stack[interface.index] try: interface.save() except DataError as error: print(error) # if if_index in tagged_ports: # for vlan in tagged_ports[if_index]: # print('vlan %s is tagged on ifindex %s' % (vlan, if_index)) # # interface.tagged_vlans.add(vlan) load_aggregations(aggregations, switch) del device.sessions[switch.ip]