theoforce 发表于 2018-7-11 08:56:31

Python 备份cisco ios 设备 简例

#get ip address from a file  ip_addr = open("G:\\PythonProject_netmiko\\ipList.txt")
  path ="G:\\PythonProject_netmiko\\"
  folder_v = os.listdir() #查看當前文件夾所有文件
  while True:
  print("input your bak file folder name:")
  folder_name = input()
  if folder_name in folder_v:
  print("there is a same name file already, please input again!")
  else:
  create_path = path + folder_name
  os.mkdir(create_path)
  print("Created folder:"+folder_name+" successfully!\n")
  print("NOTE:the bakup file will be in: ",create_path)
  break
  for ip in ip_addr:
  cisco['ip'] = ip.strip('\n')
  print('Connecting: '+ip.strip("\n")+"...")
  net_connect = ConnectHandler(**cisco)
  print("Connected successfully !")
  net_connect.enable()
  run_config = net_connect.send_command('show run')
  save_name = ip.strip() + datetime.datetime.now().strftime("-%Y%m%d-%H_%M_%S")
  print('Backing up: '+ip.strip("\n")+'...')
  f = open(create_path +"\\"+save_name+".txt",'w')
  f.write(run_config)
  f.close()
  print("Backed up successfully!")
  f = open(create_path +"\\0_IP-date-time.fileType",'w')#used to instruct
  f.close()
  ip_addr.close()
页: [1]
查看完整版本: Python 备份cisco ios 设备 简例