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 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