Guest User

Untitled

a guest
Feb 17th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import telnetlib
  2. import os
  3. import getpass
  4. import time
  5. import ipaddress
  6. import re
  7.  
  8.  
  9. def gath_ip():
  10. for ip in range(int(startIP),int(endIP)+1,1):
  11. normal_ip = str(ipaddress.IPv4Address(ip))
  12. response = os.system("ping -c 1 "+ normal_ip)
  13. if response == 0:
  14. ip_table.append(normal_ip)
  15.  
  16. def backup_sw():
  17. for switch_ip in ip_table:
  18. print(switch_ip+' status on-line')
  19. tn = telnetlib.Telnet(switch_ip, 23, 5)
  20. time.sleep(0.5)
  21. tn.read_until(b"Username: ")
  22. time.sleep(0.5)
  23. tn.write(INPUTLOGIN + ENTER)
  24. time.sleep(0.5)
  25. tn.read_until(b"Password: ")
  26. time.sleep(0.5)
  27. tn.write(PASS + ENTER)
  28. time.sleep(0.5)
  29. tn.write(b'copy running-config '+ remote_server + ENTER)
  30. time.sleep(0.5)
  31. tn.write(ENTER)
  32. time.sleep(0.5)
  33. tn.write(ENTER)
  34. information = tn.read_very_eager().decode('utf-8')
  35. time.sleep(2)
  36. load_complete = re.findall('bytes copied', information)
  37. time.sleep(0.5)
  38. while not load_complete:
  39. print('pls wait')
  40. information = tn.read_very_eager().decode('utf-8')
  41. time.sleep(2)
  42. load_complete = re.findall('bytes copied', information)
  43. time.sleep(1)
  44. else:
  45. print('download done')
  46. time.sleep(0.5)
  47. tn.write(b'exit')
  48. time.sleep(0.5)
  49. tn.write(ENTER)
  50.  
  51.  
  52. #####################ALGORITM################################
  53.  
  54. ENTER = '\n'.encode('utf-8')
  55. startIP = ipaddress.IPv4Address(input('start ip: '))
  56. endIP = ipaddress.IPv4Address(input('end ip: '))
  57. INPUTLOGIN = input('# Enter your username: ').encode('utf-8')
  58. PASS = input('# Enter your password: ').encode('utf-8')
  59. remote_server = input('# remote server (example: tftp://10.10.10.10): ').encode('utf-8')
  60. ip_table = []
  61. gath_ip()
  62. backup_sw()
Add Comment
Please, Sign In to add comment