def _run_ssh_catalyst(ip): ssh = _connect_ssh(ip) try: raw = '\n'.join(ssh.cisco_command("show inventory")) finally: ssh.close() inventory = list(cisco_inventory(raw)) serials = [inv['sn'] for inv in inventory] try: dev = Device.objects.get(sn__in=serials) except Device.DoesNotExist: dev_inv = inventory[0] dev = Device.create(sn=dev_inv['sn'], model_name='Cisco %s' % dev_inv['pid'], model_type=DeviceType.switch, name=dev_inv['descr'][:255]) dev.save(update_last_seen=True) for inv in inventory: cisco_component(dev, inv) ip_address, created = IPAddress.concurrent_get_or_create(address=str(ip)) if created: ip_address.hostname = network.hostname(ip_address.address) ip_address.device = dev ip_address.is_management = True ip_address.save(update_last_seen=True) return dev.name
def _run_ssh_catalyst(ip): ssh = _connect_ssh(ip) try: raw = "\n".join(ssh.cisco_command("show inventory")) finally: ssh.close() inventory = list(cisco_inventory(raw)) serials = [inv["sn"] for inv in inventory] try: dev = Device.objects.get(sn__in=serials) except Device.DoesNotExist: dev_inv = inventory[0] dev = Device.create( sn=dev_inv["sn"], model_name="Cisco %s" % dev_inv["pid"], model_type=DeviceType.switch, name=dev_inv["descr"][:255], ) dev.save(update_last_seen=True) for inv in inventory: cisco_component(dev, inv) ip_address, created = IPAddress.concurrent_get_or_create(address=str(ip)) if created: ip_address.hostname = network.hostname(ip_address.address) ip_address.device = dev ip_address.is_management = True ip_address.save(update_last_seen=True) return dev.name
def _run_ssh_catalyst(ip): ssh = _connect_ssh(ip) try: mac = '\n'.join(ssh.cisco_command( "show version | include Base ethernet MAC Address" )) raw = '\n'.join(ssh.cisco_command("show inventory")) finally: ssh.close() mac = mac.strip() if mac.startswith("Base ethernet MAC Address") and ':' in mac: ethernets = [ Eth( "Base ethernet MAC Address", mac.split(':', 1)[1].strip(), None, ), ] else: ethernets = None inventory = list(cisco_inventory(raw)) serials = [inv['sn'] for inv in inventory] dev_inv = inventory[0] try: dev = Device.objects.get(sn__in=serials) except MultipleObjectsReturned: raise Error( "Stacked devices with serials %r should be merged.", serials, ) except Device.DoesNotExist: sn = dev_inv['sn'] model_name='Cisco %s' % dev_inv['pid'] else: # This is a stacked device, use the base device for it sn = dev.sn model_name = dev.model.name dev = Device.create( ethernets=ethernets, sn=sn, model_name=model_name, model_type=DeviceType.switch, name=dev_inv['descr'][:255], ) dev.save(update_last_seen=True) for inv in inventory: cisco_component(dev, inv) ip_address, created = IPAddress.concurrent_get_or_create(address=str(ip)) if created: ip_address.hostname = network.hostname(ip_address.address) ip_address.device = dev ip_address.is_management = True ip_address.save(update_last_seen=True) return dev.name
def scan_address(ip_address, **kwargs): if 'nx-os' in (kwargs.get('snmp_name', '') or '').lower(): raise NoMatchError('Incompatible Nexus found.') if kwargs.get('http_family') not in ('Unspecified', 'Cisco'): raise NoMatchError('It is not Cisco.') ssh = _connect_ssh(ip_address) hostname = network.hostname(ip_address) try: ssh.cisco_command('terminal length 500') mac = '\n'.join( ssh.cisco_command( "show version | include Base ethernet MAC Address", )) raw = '\n'.join(ssh.cisco_command("show inventory")) subswitches = get_subswitches(ssh.cisco_command("show version"), hostname, ip_address) finally: ssh.close() matches = re.match('Base ethernet MAC Address\s+:\s*([0-9aA-Z:]+)', mac) if matches.groups(): mac = matches.groups()[0] inventory = list(cisco_inventory(raw)) dev_inv, parts = inventory[0], inventory[1:] sn = dev_inv['sn'] result = get_base_result_template('ssh_cisco_catalyst') if subswitches: # virtual switch doesn't have own unique id, reuse first from stack sn += '-virtual' model_name = 'Virtual Cisco Catalyst %s' % dev_inv['pid'] model_type = DeviceType.switch_stack else: model_name = 'Cisco Catalyst %s' % dev_inv['pid'] model_type = DeviceType.switch result.update({ 'status': 'success', 'device': { 'hostname': hostname if hostname else ip_address, 'model_name': model_name, 'type': model_type.raw, 'management_ip_addresses': [ip_address], 'parts': [{ 'serial_number': part['sn'], 'name': part['name'], 'label': part['descr'], } for part in parts], }, }) if sn not in SERIAL_BLACKLIST: result['device']['serial_number'] = sn if subswitches: result['device']['subdevices'] = subswitches else: result['device']['mac_addresses'] = [mac] return result
def scan_address(ip_address, **kwargs): if 'nx-os' in (kwargs.get('snmp_name', '') or '').lower(): raise NoMatchError('Incompatible Nexus found.') if kwargs.get('http_family') not in ('Unspecified', 'Cisco'): raise NoMatchError('It is not Cisco.') ssh = _connect_ssh(ip_address) hostname = network.hostname(ip_address) try: ssh.cisco_command('terminal length 500') mac = '\n'.join(ssh.cisco_command( "show version | include Base ethernet MAC Address", )) raw = '\n'.join(ssh.cisco_command("show inventory")) subswitches = get_subswitches( ssh.cisco_command("show version"), hostname, ip_address ) finally: ssh.close() matches = re.match( 'Base ethernet MAC Address\s+:\s*([0-9aA-Z:]+)', mac) if matches.groups(): mac = matches.groups()[0] inventory = list(cisco_inventory(raw)) dev_inv, parts = inventory[0], inventory[1:] sn = dev_inv['sn'] result = get_base_result_template('ssh_cisco_catalyst') if subswitches: # virtual switch doesn't have own unique id, reuse first from stack sn += '-virtual' model_name = 'Virtual Cisco Catalyst %s' % dev_inv['pid'] model_type = DeviceType.switch_stack else: model_name = 'Cisco Catalyst %s' % dev_inv['pid'] model_type = DeviceType.switch result.update({ 'status': 'success', 'device': { 'hostname': hostname if hostname else ip_address, 'model_name': model_name, 'type': model_type.raw, 'management_ip_addresses': [ip_address], 'parts': [{ 'serial_number': part['sn'], 'name': part['name'], 'label': part['descr'], } for part in parts], }, }) if sn not in SERIAL_BLACKLIST: result['device']['serial_number'] = sn if subswitches: result['device']['subdevices'] = subswitches else: result['device']['mac_addresses'] = [MACAddressField.normalize(mac)] return result
def run_ssh_asa(ip): ssh = _connect_ssh(ip) try: lines = ssh.asa_command( "show version | grep (^Hardware|Boot microcode|^Serial|address is)" ) raw_inventory = '\n'.join(ssh.asa_command("show inventory")) finally: ssh.close() pairs = parse.pairs(lines=[line.strip() for line in lines]) sn = pairs.get('Serial Number', None) model, ram, cpu = pairs['Hardware'].split(',') boot_firmware = pairs['Boot microcode'] ethernets = [] for i in xrange(99): try: junk, label, mac = pairs['%d' % i].split(':') except KeyError: break mac = mac.split(',', 1)[0] mac = mac.replace('address is', '') mac = mac.replace('.', '').upper().strip() label = label.strip() ethernets.append(Eth(label, mac, speed=None)) dev = Device.create(ethernets=ethernets, sn=sn, model_name=model, model_type=DeviceType.firewall, boot_firmware=boot_firmware) dev.save(update_last_seen=True) inventory = list(cisco_inventory(raw_inventory)) for inv in inventory: cisco_component(dev, inv) ipaddr, created = IPAddress.concurrent_get_or_create(address=ip) ipaddr.device = dev ipaddr.is_management = True ipaddr.save() for label, mac, speed in ethernets: eth, created = Ethernet.concurrent_get_or_create( mac=mac, defaults={'device': dev}, ) eth.label = label eth.device = dev eth.save() return model
def scan_address(ip_address, **kwargs): status = 'success' ssh = _connect_ssh(ip_address) try: mac = '\n'.join( ssh.cisco_command( "show version | include Base ethernet MAC Address", )) raw = '\n'.join(ssh.cisco_command("show inventory")) finally: ssh.close() mac = mac.strip() if mac.startswith("Base ethernet MAC Address") and ':' in mac: mac = mac.split(':', 1)[1].strip().replace(":", "") else: ethernets = None inventory = list(cisco_inventory(raw)) serials = [inv['sn'] for inv in inventory] dev_inv = inventory[0] model_name = 'Cisco Catalyst %s' % dev_inv['pid'] sn = dev_inv['sn'] model_type = DeviceType.switch device = { 'hostname': network.hostname(ip_address), 'model_name': model_name, 'type': str(model_type), 'serial_number': sn, 'mac_adresses': [ mac, ], 'management_ip_addresses': [ ip_address, ], } parts = inventory[1:] device['parts'] = [] for p in parts: part = { 'serial_number': p['sn'], 'name': p['name'], 'label': p['descr'], } device['parts'].append(part) ret = { 'status': status, 'device': device, } tpl = get_base_result_template('ssh_cisco_catalyst') tpl.update(ret) return tpl
def scan_address(ip_address, **kwargs): status = 'success' ssh = _connect_ssh(ip_address) try: mac = '\n'.join(ssh.cisco_command( "show version | include Base ethernet MAC Address", )) raw = '\n'.join(ssh.cisco_command("show inventory")) finally: ssh.close() mac = mac.strip() if mac.startswith("Base ethernet MAC Address") and ':' in mac: mac = mac.split(':', 1)[1].strip().replace(":", "") else: ethernets = None inventory = list(cisco_inventory(raw)) serials = [inv['sn'] for inv in inventory] dev_inv = inventory[0] model_name='Cisco Catalyst %s' % dev_inv['pid'] sn = dev_inv['sn'] model_type=DeviceType.switch device = { 'hostname': network.hostname(ip_address), 'model_name': model_name, 'type': str(model_type), 'serial_number': sn, 'mac_adresses': [mac, ], 'management_ip_addresses': [ip_address, ], } parts = inventory[1:] device['parts'] = [] for p in parts: part = { 'serial_number': p['sn'], 'name': p['name'], 'label': p['descr'], } device['parts'].append(part) ret = { 'status': status, 'device': device, } tpl = get_base_result_template('ssh_cisco_catalyst') tpl.update(ret) return tpl