我对Python还是很陌生,过去几天我花了无数小时来调试这个特定问题。我有一个脚本,可以使用多处理和netmiko一次备份到多个网络设备。下面是一个片段。
print '\n\nRunning Backup for ' + device_location
devices = READ_DEVICES('Files/Devices/' + device_location + '.txt')
starting_time = time()
processes = []
queue1 = Queue()
queue2 = Queue()
queue3 = Queue()
queue4 = Queue()
for ipaddr,device in devices.items():
print '\nConnecting to', device['name']
address, name, types, location = device['ipaddr'], device['name'], device['type'], device['location']
queue1.put(address)
queue2.put(name)
queue3.put(types)
queue4.put(location)
processes.append(Process(target = DO_BACKUP, args = (queue1,queue2,queue3,queue4)))
for p in processes:
p.start()
for p in processes:
p.join()
print '\n\nBackup Complete: \n\nFile available at GNS Jumpbox (10.22.32.47) - D:/GNS/Backups/' + date_today + '/' + device_location + ' folder and in your local directory at the Outputs/Backups folder.' + '\n\n'
print '\nElapsed Time: ', time() - starting_time, '\n\n'enter
def DO_BACKUP(address, name, types, location):
try:
ip_address, device_type, hostname = CONFIG_WORKER(address, name, types, location)
if device_type == 'cisco_ios':
print 'Retrieving Data from IOS Device', hostname
backup_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/ios_backup_commands.txt', hostname)
healthcheck_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/ios_healthcheck_commands.txt', hostname)
logging_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/ios_logging_commands.txt', hostname)
elif device_type == 'cisco_asa':
print 'Retrieving Firewall Data from ASA', hostname
backup_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/asa_backup_commands.txt', hostname)
healthcheck_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/asa_healthcheck_commands.txt', hostname)
logging_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/asa_logging_commands.txt', hostname)
elif device_type == 'cisco_nxos':
print 'Retrieving Switch Data from Nexus', hostname
backup_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/nxos_backup_commands.txt', hostname)
healthcheck_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/nxos_healthcheck_commands.txt', hostname)
logging_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/nxos_logging_commands.txt', hostname)
elif device_type == 'fortinet':
print 'Retrieving Firewall Data from Fortigate', hostname
backup_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/fortinet_backup_commands.txt', hostname)
healthcheck_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/fortinet_healthcheck_commands.txt', hostname)
logging_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/fortinet_logging_commands.txt', hostname)
elif device_type == 'cisco_wlc':
print 'Retrieving WLC Data from Device', hostname
backup_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/wlc_backup_commands.txt', hostname)
healthcheck_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/wlc_healthcheck_commands.txt', hostname)
logging_data = NETMIKO_SHOW_COMMAND('Files/Commands/Backup/Current/wlc_logging_commands.txt', hostname)
print 'Saving Configuration of', hostname, '\n'
with open( 'Outputs/Backups/' + hostname + '-config.txt', 'w' ) as config_out: config_out.write(backup_data + stamps)
with open( 'Outputs/Backups/' + hostname + '-healthcheck.txt', 'w' ) as config_out: config_out.write(healthcheck_data + stamps)
with open( 'Outputs/Backups/' + hostname + '-logs.txt', 'w' ) as config_out: config_out.write(logging_data + stamps)
脚本运行,并且运行到最后没有错误,但没有执行目标功能。请注意,此脚本运行平稳,无需进行多处理。
下面是脚本的输出。
马尼拉的运行备份
连接到4th_SWB1
连接到PH-MNL-FW-EXT-01
连接到imgmax02
...
备份完成:
可在GNS Jumpbox上找到的文件-D:/ GNS / Backups / 2018-06-22 / Manila文件夹,以及本地目录中的Outputs / Backups文件夹。
经过时间:2.40400004387