Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. '''
  4. This script backups your 3COM
  5. switch configuration data. It
  6. needs a TFTP service running!
  7.  
  8. Usage: saveconf_3C3800 <host>
  9.  
  10. It works with:
  11. - 3COM 3870 (3CR17451-91)
  12. - 3COM SuperStack 3824 (?)
  13. - 3COM 4400PWR (3C17205)
  14. '''
  15.  
  16. import sys
  17. import telnetlib
  18.  
  19. ### EDIT ###
  20. username = "admin"
  21. password = "mypassword"
  22. tftp = '192.168.1.14'
  23. #############
  24.  
  25. telnet_timeout = 1
  26.  
  27. def connect(ipaddress, username, password):
  28. try:
  29. tn = telnetlib.Telnet(ipaddress)
  30. try:
  31. #print "Authentication..."
  32. tn.read_until("Login:", telnet_timeout)
  33. tn.write(username + "\r")
  34. tn.read_until("Password:", telnet_timeout)
  35. tn.write(password + "\r")
  36. tn.read_until("Select menu option:", telnet_timeout)
  37. #print "OK!"
  38. return tn
  39. except Exception, e:
  40. print "Incorrect user/password"
  41. print "\nException: %s" % str(e)
  42. tn.close()
  43. except Exception, e:
  44. print "Error connecting to " + ipaddress
  45. print "\nException: %s" % str(e)
  46.  
  47.  
  48. def disconnect(tn):
  49. try:
  50. tn.write("logout\r\n")
  51. tn.close()
  52. except Exception, e:
  53. print "Error: connection problem or model not supported"
  54. print "\nException: %s" % str(e)
  55.  
  56.  
  57. def summary(tn):
  58. prompt = "Select menu option:"
  59. try:
  60. tn.write("system summary\r\n")
  61. tn.write("\r\n") #some models need it :S
  62. output = tn.read_until(prompt, telnet_timeout)
  63. except Exception, e:
  64. print "Error connection problem or model not supported"
  65. print "\nException: %s" % str(e)
  66.  
  67. data = {}
  68. for line in output.splitlines():
  69. if ':' in line:
  70. key, value = line.partition(':')[::2]
  71. key = key.strip()
  72. value = value.strip()
  73. if value is not '':
  74. data[key] = value
  75. tn.read_until(prompt, telnet_timeout)
  76. return data
  77.  
  78.  
  79. def backup(tn, tftp, filename):
  80. print "TFTP: " + tftp
  81. print "File: " + filename
  82. prompt = "Select menu option"
  83. print "Backing up..."
  84. try:
  85. tn.write("system backupConfig save %s %s\r\n" % (tftp, filename))
  86. tn.write("\r\n")
  87. output = tn.read_until(prompt, 120)
  88. except Exception, e:
  89. print "Error: connection problem or model not supported"
  90. print "\nException: %s" % str(e)
  91.  
  92. if "bytes transferred" in output:
  93. return True
  94. elif "Save of system configuration Successful" in output:
  95. return True
  96. else:
  97. print "Error #5: can't connect to TFTP server"
  98. return False
  99.  
  100.  
  101. if __name__ == "__main__":
  102. if len(sys.argv) > 1:
  103. ipaddress = sys.argv[1]
  104. else:
  105. print "Usage: %s <host>" % sys.argv[0]
  106. sys.exit(1)
  107.  
  108. tn = connect(ipaddress, username, password)
  109.  
  110. if not tn:
  111. sys.exit(1)
  112.  
  113. s = summary(tn)
  114. if s['Product Number'] != '3C17205' and s['Product Number'] != '3CR17451-91':
  115. print "Warning: Model not tested: %s" % s['Product Number']
  116. print "Continuing..."
  117. #sys.exit(1)
  118.  
  119. filename = s['System Name'] + '.cfg'
  120.  
  121. if backup(tn, tftp, filename):
  122. print "Backup created succesfully!"
  123. disconnect(tn)
  124. sys.exit(0)
  125. else:
  126. print "Error: Backup failed!"
  127. disconnect(tn)
  128. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement