daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Dec 18th, 2018 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import time
  3. import paramiko
  4. import os
  5. import cmd
  6. import datetime
  7.  
  8.  
  9.  
  10. #set date and time
  11. now = datetime.datetime.now()
  12.  
  13. #authentication
  14. HOST = open('d:pythonhosts.txt')
  15. USER = 'user'
  16. PASSWORD = 'password'
  17. secret = 'password'
  18.  
  19. all_ips = [ip.rstrip() for ip in HOST]
  20.  
  21.  
  22. #prefix files for backup
  23. filename_prefix ='cisco_backup'+ip
  24.  
  25. print all_ips
  26. #ssh session starts
  27. client = paramiko.SSHClient()
  28. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  29. client.connect(ip, username=USER, password=PASSWORD)
  30.  
  31. #ssh shell
  32. chan = client.invoke_shell()
  33. time.sleep(1)
  34. #enter enable secret
  35. chan.send('enn')
  36. chan.send(secret +'n')
  37. time.sleep(1)
  38. #terminal lenght for no paging
  39. chan.send('term len 0n')
  40. time.sleep(1)
  41. #show config and write output
  42. chan.send('sh runn')
  43. time.sleep(10)
  44. output = chan.recv(99999)
  45. #show output config and write file with prefix, date and time
  46. print output
  47. filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)
  48. f = open(filename, 'a')
  49. f.write(output)
  50. f.close()
  51. #close ssh session
  52. client.close()
  53.    
  54. for ip in hosts:
  55.     filename_prefix = 'cisco_backup' + ip
  56.     # И прочие ваши действия с элементом списка
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top