Guest User

Untitled

a guest
Mar 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1.  
  2. import optparse
  3. import pexpect
  4. import sys
  5. import time
  6.  
  7. from optparse import OptionParser
  8. from time import localtime, strftime
  9.  
  10. #
  11. # General
  12. SSH_BIN = '/usr/bin/ssh'
  13. DEBUG = True
  14. NO_DATE = False
  15.  
  16. #
  17. # SSH Expected Prompt Configuration
  18. SSH_PASSWORD_PROMPT = '.*password: '
  19. SSH_PASSWORD_FAILED_TEXT = '.*password: '
  20. SSH_LOGIN_SUCCEESS_TEXT = '.*-> '
  21.  
  22. #
  23. # Commands to be sent
  24. JUNIPER_SAVE_CONFIG_CMD_PREFIX = 'save config from flash to'
  25.  
  26. DEVICE_USERNAME = 'admin'
  27. DEVICE_PASSWORD = ''
  28. DEVICE_ADDRESS = ''
  29.  
  30. EXIT_CODE_FAIL = 1
  31. EXIT_CODE_SUCCESS = 0
  32.  
  33. #
  34. # Function Defs.
  35. # Wheeeeeeeee......
  36. #
  37. def build_ssh_command( user = DEVICE_USERNAME, \
  38. pasw = DEVICE_PASSWORD, \
  39. addr = DEVICE_ADDRESS ):
  40. if (not addr):
  41. raise ValueError("No address specified!")
  42. elif (user):
  43. destination = user + '@' + addr
  44. else:
  45. destination = addr
  46.  
  47. # This could probably be done in a much safer, much more
  48. # intelligent way, but for now, concat for the win!
  49. command = SSH_BIN + ' ' + dest
  50. return command
  51.  
  52. def perform_expect_command( expect_child, \
  53. expect_line = '', \
  54. response_line = '', \
  55. timeout = 0 ):
  56.  
  57. if ( timeout > 0):
  58. expect_child.expect(expect_line, timeout=timeout)
  59. else:
  60. expect_child.expect(expect_line)
  61.  
  62. expect_child.sendline(response_line)
  63.  
  64.  
  65. # Command line parsing
  66. parser = OptionParser()
  67.  
  68. parser.add_option("-H", "--hostname", dest="address")
  69. parser.add_option("-u", "--username", dest="username")
  70. parser.add_option("-p", "--password", dest="password")
  71. parser.add_option("-t", "--tftp-address", dest="tftp-address")
  72. parser.add_option("-f", "--filename", dest="filename")
  73. parser.add_option("-d", "--debug", dest="DEBUG", action="store_true")
  74. parser.add_option("-D", "--no-date", dest="NO_DATE", action="store_true")
  75.  
  76. (options, args) = parser.parse_args()
  77.  
  78.  
  79. ####### ssh_proc = pexpect.spawn ( build_ssh_command
  80.  
  81. try:
  82. cmd = build_ssh_command( options['username'], options['password'], options['address'] )
  83. print cmd
  84. except ValueError:
  85. print str(ValueError)
  86. sys.exit(EXIT_CODE_FAIL)
  87.  
  88. #####################
  89. Error:
  90. Traceback (most recent call last):
  91. File "./juniper-tftp-backup.py", line 97, in <module>
  92. cmd = build_ssh_command( options['username'], options['password'], options['address'] )
  93. AttributeError: Values instance has no attribute '__getitem__'
Add Comment
Please, Sign In to add comment