Guest User

Untitled

a guest
Apr 12th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import netmiko
  4. from netmiko import SCPConn
  5. import os,sys,commands,re
  6.  
  7.  
  8. def check_md5(filename):
  9. command = 'md5sum '+filename
  10. o = commands.getoutput(command)
  11. output = o.split(' ')
  12. return output[0]
  13.  
  14. def set_boot(net_connect,file):
  15. net_connect.config_mode()
  16. net_connect.send_command('boot system flash:{}'.format(file))
  17. net_connect.exit_config_mode()
  18. net_connect.send_command('wr')
  19.  
  20. def reload(net_connect):
  21. net_connect.send_command('reload',expect_string='')
  22. net_connect.send_command('\n')
  23.  
  24. def archive_run(net_connect,filename):
  25. result = net_connect.send_command("show run")
  26. # close SSH connection
  27. net_connect.disconnect()
  28. file = open(filename,"w")
  29. file.write(result)
  30. file.close()
  31.  
  32. def verify_md5(net_connect,file,md5):
  33. result = net_connect.send_command("verify /md5 flash:{} {}".format(file,md5))
  34. # close SSH connection
  35. net_connect.disconnect()
  36. reg = re.compile(r'Verified')
  37. verify = reg.findall(result)
  38. if verify:
  39. result = True
  40. else:
  41. result = False
  42. return result
  43.  
  44. def verify_space(net_connect,file):
  45. result = net_connect.send_command("show flash:")
  46. # close SSH connection
  47. net_connect.disconnect()
  48. reg = re.compile(r'(\d+)\sbytes\sfree')
  49. space = reg.findall(result)
  50. reg = re.compile(r'.*-rwx.*({})'.format(file))
  51. exist = reg.findall(result)
  52. f_size = os.path.getsize(file)
  53. if space >= f_size:
  54. result = 'True'
  55. if space < f_size:
  56. result = 'False'
  57. if exist:
  58. exist = 'True'
  59. else:
  60. exist = 'False'
  61. return result,exist
  62.  
  63. def transfer_file(net_connect,file):
  64. net_connect.config_mode()
  65. net_connect.send_command('ip scp server enable')
  66. scp_conn = SCPConn(net_connect)
  67. s_file = file
  68. d_file = file
  69. scp_conn.scp_transfer_file(s_file, d_file)
  70.  
  71.  
  72. if __name__ == "__main__":
  73. if len(sys.argv) != 5:
  74. print("\nplease provide the following arguments:")
  75. print("\tupgrade_cisco.py <ip> <username> <password> <file>\n\n")
  76. print("\tsample usage 1: upgrade_cisco.py 192.168.10.100 admin password cisco_ios_file.bin\n\n")
  77. sys.exit(0)
  78.  
  79. ip = sys.argv[1]
  80. username = sys.argv[2]
  81. password = sys.argv[3]
  82. file_s = sys.argv[4]
  83.  
  84. #verify if there is enough free space on device to upload ios file
  85. net_connect = netmiko.ConnectHandler(device_type='cisco_ios', ip=ip, username=username, password=password)
  86. ver = verify_space(net_connect,file_s)
  87.  
  88. print "\n\nVerifying sufficient space available on the file system ... %s\n\n" % ip
  89.  
  90. if ver[0] == 'True' and ver[1] == 'False':
  91. upload = raw_input("\n\nSuccess! - proceed with image upload ? (y/n) ... ")
  92. if upload == 'y':
  93. print "\n\nUploading file : %s ...\n\n" % file_s
  94.  
  95. #transferring file to device
  96. net_connect = netmiko.ConnectHandler(device_type='cisco_ios', ip=ip, username=username, password=password)
  97. transfer_file(net_connect,file_s)
  98. print "\n\nSuccess! - upload file: %s to device: %s was successfull ... \n\n" % (file_s,ip)
  99.  
  100. #veryfing md5
  101. md5 = check_md5(file_s)
  102. print "\n\nVerifying md5 checksum on device ... %s\n\n" % ip
  103. net_connect = netmiko.ConnectHandler(device_type='cisco_ios', ip=ip, username=username, password=password)
  104. v_md5 = verify_md5(net_connect, file_s,md5)
  105. if v_md5 == True:
  106. archive = raw_input("\n\nSuccess! - proceed with archive running config ? (y/n) ... ")
  107. if archive == 'y':
  108. file_n = ip+"-running-config.txt"
  109. print "\n\nSaving running config into file: %s \n\n" % file_n
  110. net_connect = netmiko.ConnectHandler(device_type='cisco_ios', ip=ip, username=username, password=password)
  111. archive_run(net_connect,file_n)
  112.  
  113. boot = raw_input("\n\nSuccess! - proceed with inserting boot system command ? (y/n) ... ")
  114. if boot == 'y':
  115. net_connect = netmiko.ConnectHandler(device_type='cisco_ios', ip=ip, username=username, password=password)
  116. set_boot(net_connect, file_s)
  117.  
  118. reload_3 = raw_input("\n\nSuccess! - proceed with reload ? (y/n) ... ")
  119. if reload_3 == 'y':
  120. reload_2 = raw_input("\n\nAre you sure ? (Y\N) ... ")
  121. if reload_2 == 'Y':
  122. net_connect = netmiko.ConnectHandler(device_type='cisco_ios', ip=ip, username=username, password=password)
  123. try:
  124. reload(net_connect)
  125. except:
  126. print "Reloading ... "
  127. else:
  128. print "\n\nAbort !!!\n\n"
  129. else:
  130. print "\n\nAbort !!!\n\n"
  131.  
  132. else:
  133. print "\n\nError veryfiing md5 checksum on device, quitting !!!\n\n"
  134.  
  135. elif ver[0] == 'False' and ver[1] == 'False':
  136. print "\n\nNot enough free space on device ... %s \n\n" % ip
  137.  
  138. elif ver[1] == 'True':
  139. print "\n\nFile already uploaded on device ... %s \n\n" % ip
Add Comment
Please, Sign In to add comment