def retrieve_config(): ''' Use SSH to retrieve the network device running configuration. ''' DEBUG = True net_devices = NetworkDevice.objects.all() for a_device in net_devices: if 'ssh' in a_device.device_class: if DEBUG: print "Retrieve device configuration: {} {}\n".format( a_device.device_name, a_device.device_class) ssh_connect = SSHConnection(a_device) ssh_connect.enable_mode() output = ssh_connect.send_command('show run\n') file_name = a_device.device_name + '.txt' full_path = CFGS_DIR + file_name if DEBUG: print "Writing configuration file to file system\n" with open(full_path, 'w') as f: f.write(output)
def retrieve_config(): ''' Use SSH to retrieve the network device running configuration. ''' DEBUG = True net_devices = NetworkDevice.objects.all() for a_device in net_devices: if 'ssh' in a_device.device_class: if DEBUG: print "Retrieve device configuration: {} {}\n".format(a_device.device_name, a_device.device_class) ssh_connect = SSHConnection(a_device) ssh_connect.enable_mode() output = ssh_connect.send_command('show run\n') file_name = a_device.device_name + '.txt' full_path = CFGS_DIR + file_name if DEBUG: print "Writing configuration file to file system\n" with open(full_path, 'w') as f: f.write(output)
def backup_config(a_device): DEBUG = True perform_diff = False if DEBUG: print "Retrieve device configuration via SSH: {}\n".format(a_device.device_name) ssh_connect = SSHConnection(a_device) ssh_connect.enable_mode() output = ssh_connect.send_command('show run\n') file_name = a_device.device_name + '.txt' full_path = global_params.CFGS_DIR + file_name bup_file = global_params.CFGS_DIR + a_device.device_name + '.old' # Check if file already exists if os.path.isfile(full_path): # Create copy of old file cmd_status = subprocess.call(['/bin/mv', full_path, bup_file]) perform_diff = True if DEBUG: print "Writing configuration file to file system\n" with open(full_path, 'w') as f: f.write(output) a_device.cfg_file = file_name a_device.cfg_archive_time = timezone.make_aware(datetime.now(), timezone.get_current_timezone()) # obtain last_changed time (Cisco specific) a_device.cfg_last_changed = int(snmp_wrapper(a_device, oid=global_params.OID_RUNNING_LAST_CHANGED)) a_device.save() if perform_diff: return find_diff(full_path, bup_file) else: return None
def inventory_dispatcher(): ''' Dispatcher for calling SSH, onePK, or eAPI based on the NetworkDevice.device_class ''' DEBUG = True # Single location to specify the relevant GatherInventory class to use class_mapper = { 'cisco_ios_ssh': CiscoGatherInventory, 'arista_eos_ssh': AristaGatherInventory, } net_devices = NetworkDevice.objects.all() for a_device in net_devices: if 'ssh' in a_device.device_class: if DEBUG: print "SSH inventory call: {} {}\n".format( a_device.device_name, a_device.device_class) ssh_connect = SSHConnection(a_device) output = ssh_connect.send_command('show version\n') inventory_obj = class_mapper[a_device.device_class](a_device, output) inventory_obj.find_vendor() inventory_obj.find_model() inventory_obj.find_device_type() inventory_obj.find_os_version() inventory_obj.find_serial_number() inventory_obj.find_uptime() print 'Inventory gathering for device complete' print_inventory(a_device) elif 'onepk' in a_device.device_class: if DEBUG: print "onePK inventory call: {} {}\n".format( a_device.device_name, a_device.device_class) pass elif 'eapi' in a_device.device_class: if DEBUG: print "eAPI inventory call: {} {}\n".format( a_device.device_name, a_device.device_class) pass else: # invalid condition / exception pass
def inventory_dispatcher(): ''' Dispatcher for calling SSH, onePK, or eAPI based on the NetworkDevice.device_class ''' DEBUG = True # Single location to specify the relevant GatherInventory class to use class_mapper = { 'cisco_ios_ssh' : CiscoGatherInventory, 'arista_eos_ssh' : AristaGatherInventory, } net_devices = NetworkDevice.objects.all() for a_device in net_devices: if 'ssh' in a_device.device_class: if DEBUG: print "SSH inventory call: {} {}\n".format(a_device.device_name, a_device.device_class) ssh_connect = SSHConnection(a_device) output = ssh_connect.send_command('show version\n') inventory_obj = class_mapper[a_device.device_class](a_device, output) inventory_obj.find_vendor() inventory_obj.find_model() inventory_obj.find_device_type() inventory_obj.find_os_version() inventory_obj.find_serial_number() inventory_obj.find_uptime() print 'Inventory gathering for device complete' print_inventory(a_device) elif 'onepk' in a_device.device_class: if DEBUG: print "onePK inventory call: {} {}\n".format(a_device.device_name, a_device.device_class) pass elif 'eapi' in a_device.device_class: if DEBUG: print "eAPI inventory call: {} {}\n".format(a_device.device_name, a_device.device_class) pass else: # invalid condition / exception pass
def backup_config(a_device): ''' Retrieve configuration from network device, save to filesystem. ''' DEBUG = True perform_diff = False if DEBUG: print "Retrieve device configuration via SSH: {}\n".format( a_device.device_name) ssh_connect = SSHConnection(a_device) ssh_connect.enable_mode() output = ssh_connect.send_command('show run\n') file_name = a_device.device_name + '.txt' full_path = global_params.CFGS_DIR + file_name bup_file = global_params.CFGS_DIR + a_device.device_name + '.old' # Check if file already exists if os.path.isfile(full_path): # Create copy of old file cmd_status = subprocess.call(['/bin/mv', full_path, bup_file]) perform_diff = True if DEBUG: print "Writing configuration file to file system\n" with open(full_path, 'w') as f: f.write(output) a_device.cfg_file = file_name a_device.cfg_archive_time = timezone.make_aware( datetime.now(), timezone.get_current_timezone()) # obtain last_changed time (Cisco specific) a_device.cfg_last_changed = int( snmp_wrapper(a_device, oid=global_params.OID_RUNNING_LAST_CHANGED)) a_device.save() if perform_diff: return find_diff(full_path, bup_file) else: return None
def inventory_dispatcher(): ''' Dispatcher for calling SSH, onePK, or eAPI based on the NetworkDevice.device_class ''' DEBUG = True net_devices = NetworkDevice.objects.all() for a_device in net_devices: if 'ssh' in a_device.device_class: if DEBUG: print "SSH inventory call: {} {}\n".format(a_device.device_name, a_device.device_class) ssh_connect = SSHConnection(a_device) output = ssh_connect.send_command('show version\n') inventory_obj = CLASS_MAPPER[a_device.device_class](a_device, output) inventory_obj.find_vendor() inventory_obj.find_model() inventory_obj.find_device_type() inventory_obj.find_os_version() inventory_obj.find_serial_number() inventory_obj.find_uptime() print 'Inventory gathering for device complete' print_inventory(a_device) elif 'onepk' in a_device.device_class: if DEBUG: print "onePK inventory call: {} {}\n".format(a_device.device_name, a_device.device_class) # FIX - pin_file is hard-coded onepk_connect = onepk_helper.NetworkDevice( ip=a_device.ip_address, username=a_device.credentials.username, password=a_device.credentials.password, port=a_device.api_port, pin_file='pynet-rtr1-pin.txt' ) onepk_connect.establish_session() a_device.vendor = 'cisco' part_number = onepk_connect.net_element.properties.product_id a_device.model = onepk_find_model(part_number) a_device.device_type = onepk_find_device_type(a_device.model) sys_descr = onepk_connect.net_element.properties.sys_descr a_device.os_version = onepk_find_os_version(sys_descr) a_device.serial_number = onepk_connect.net_element.properties.SerialNo a_device.uptime_seconds = onepk_connect.net_element.properties.sys_uptime a_device.save() onepk_connect.disconnect() print 'Inventory gathering for device complete' print_inventory(a_device) elif 'eapi' in a_device.device_class: if DEBUG: print "eAPI inventory call: {} {}\n".format(a_device.device_name, a_device.device_class) eapi_conn = eapilib.create_connection( hostname=a_device.ip_address, username=a_device.credentials.username, password=a_device.credentials.password, port=a_device.api_port ) response = eapi_conn.run_commands(['show version']) arista_dict = response[0] a_device.vendor = 'arista' a_device.model = arista_dict['modelName'] # Should really be a function if a_device.model == 'vEOS': a_device.device_type = 'switch' else: a_device.device_type = '' a_device.os_version = arista_dict['version'] a_device.serial_number = arista_dict['serialNumber'] # Should normalize the MacAddress format if not a_device.serial_number: a_device.serial_number = arista_dict['systemMacAddress'] # bootupTimestamp is since epoch. Requires time on router to be right. uptime_seconds = arista_dict['bootupTimestamp'] time_delta = time.time() - int(uptime_seconds) a_device.uptime_seconds = int(time_delta) a_device.save() print 'Inventory gathering for device complete' print_inventory(a_device) else: # invalid condition / exception pass
def inventory_dispatcher(): ''' Dispatcher for calling SSH, onePK, or eAPI based on the NetworkDevice.device_class ''' DEBUG = True net_devices = NetworkDevice.objects.all() for a_device in net_devices: if 'ssh' in a_device.device_class: if DEBUG: print "SSH inventory call: {} {}\n".format( a_device.device_name, a_device.device_class) ssh_connect = SSHConnection(a_device) output = ssh_connect.send_command('show version\n') inventory_obj = CLASS_MAPPER[a_device.device_class](a_device, output) inventory_obj.find_vendor() inventory_obj.find_model() inventory_obj.find_device_type() inventory_obj.find_os_version() inventory_obj.find_serial_number() inventory_obj.find_uptime() print 'Inventory gathering for device complete' print_inventory(a_device) elif 'onepk' in a_device.device_class: if DEBUG: print "onePK inventory call: {} {}\n".format( a_device.device_name, a_device.device_class) # FIX - pin_file is hard-coded onepk_connect = onepk_helper.NetworkDevice( ip=a_device.ip_address, username=a_device.credentials.username, password=a_device.credentials.password, port=a_device.api_port, pin_file='pynet-rtr1-pin.txt') onepk_connect.establish_session() a_device.vendor = 'cisco' part_number = onepk_connect.net_element.properties.product_id a_device.model = onepk_find_model(part_number) a_device.device_type = onepk_find_device_type(a_device.model) sys_descr = onepk_connect.net_element.properties.sys_descr a_device.os_version = onepk_find_os_version(sys_descr) a_device.serial_number = onepk_connect.net_element.properties.SerialNo a_device.uptime_seconds = onepk_connect.net_element.properties.sys_uptime a_device.save() onepk_connect.disconnect() print 'Inventory gathering for device complete' print_inventory(a_device) elif 'eapi' in a_device.device_class: if DEBUG: print "eAPI inventory call: {} {}\n".format( a_device.device_name, a_device.device_class) eapi_conn = eapilib.create_connection( hostname=a_device.ip_address, username=a_device.credentials.username, password=a_device.credentials.password, port=a_device.api_port) response = eapi_conn.run_commands(['show version']) arista_dict = response[0] a_device.vendor = 'arista' a_device.model = arista_dict['modelName'] # Should really be a function if a_device.model == 'vEOS': a_device.device_type = 'switch' else: a_device.device_type = '' a_device.os_version = arista_dict['version'] a_device.serial_number = arista_dict['serialNumber'] # Should normalize the MacAddress format if not a_device.serial_number: a_device.serial_number = arista_dict['systemMacAddress'] # bootupTimestamp is since epoch. Requires time on router to be right. uptime_seconds = arista_dict['bootupTimestamp'] time_delta = time.time() - int(uptime_seconds) a_device.uptime_seconds = int(time_delta) a_device.save() print 'Inventory gathering for device complete' print_inventory(a_device) else: # invalid condition / exception pass