Guest User

Untitled

a guest
Jan 16th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import napalm, re, datetime, os
  4.  
  5. path = os.getcwd()
  6.  
  7. try:
  8. os.stat(path+'/backup_config')
  9. except:
  10. os.mkdir(path+'/backup_config')
  11.  
  12. driver = napalm.get_network_driver('ios')
  13.  
  14. ### device list to backup configuration
  15. device_list = ['192.168.10.101','192.168.10.102']
  16.  
  17. ### username and password
  18. user = 'admin'
  19. passwd = 'cisco123'
  20.  
  21. for ip in device_list:
  22.  
  23. device = driver(hostname=ip, username=user, password=passwd)
  24. device.open()
  25. config = device.get_config(retrieve='running')
  26. facts = device.get_facts()
  27.  
  28. run_conf = config['running']
  29. #erase lines with "Building configuration", "Current Configuration" and "end"
  30. run_config = re.sub(r'Building configuration.*|Current configuration.*|end','',run_conf)
  31.  
  32. date = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
  33. hostname = facts['hostname']
  34.  
  35. ### create file with running config in backup_config folder
  36. file = open(path+'/backup_config/'+hostname+'_'+date+'_'+'running-config','w')
  37. file.write(run_config)
  38. file.close()
  39. device.close()
Add Comment
Please, Sign In to add comment