from netmiko import ConnectHandler import time from concurrent.futures import ThreadPoolExecutor, as_completed import os import subprocess # /data/tftpboot hostip = [ '10.32.4.71',##交换机ip地址 '10.32.4.72', ] flie_time =time.strftime('%Y-%m-%d') ##创建当天文件夹名称的目录 2022-05-23 base_path = '/data' path = base_path + '/' + flie_time #'/data/日期 os.makedirs(path, exist_ok=True) # os.mknod(path + '/' + flie_time) ##创建错误信息的文件存放位置 os.mknod(path + '/' + flie_time+'_err') ##创建错误信息的文件存放位置 def H3C_Command(ip): try: H3C = { 'device_type':'hp_comware', 'host':ip, 'username':'用户, 'password':'密码' } net_connect = ConnectHandler(**H3C) file_name = flie_time + '_' + ip
output = net_connect.send_command_timing('\r save \r\r y\r\ry\r\rtftp 服务器ip put startup.cfg {0} \rquit\r'.format(file_name))
except Exception as e: with open(path + '/' + flie_time+'_err', "a+") as f: f.write(ip + "不存在或者登录不上" + "\n") time.sleep(0.2) def task2(H3C_Command): data_list2 = [] executor = ThreadPoolExecutor(max_workers=10) all_task = [executor.submit(H3C_Command, (url)) for url in hostip] for future in as_completed(all_task): data_list2.append(future.result()) return data_list2 task2(H3C_Command) time.sleep(60) command_flie = 'mv -f /data/tftpboot/{0}* /data/{0} '.format(flie_time) subprocess.call(command_flie,shell=True)