Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import optparse
- import pexpect
- import sys
- import time
- from optparse import OptionParser
- from time import localtime, strftime
- #
- # General
- SSH_BIN = '/usr/bin/ssh'
- DEBUG = True
- NO_DATE = False
- #
- # SSH Expected Prompt Configuration
- SSH_PASSWORD_PROMPT = '.*password: '
- SSH_PASSWORD_FAILED_TEXT = '.*password: '
- SSH_LOGIN_SUCCEESS_TEXT = '.*-> '
- #
- # Commands to be sent
- JUNIPER_SAVE_CONFIG_CMD_PREFIX = 'save config from flash to'
- DEVICE_USERNAME = 'admin'
- DEVICE_PASSWORD = ''
- DEVICE_ADDRESS = ''
- EXIT_CODE_FAIL = 1
- EXIT_CODE_SUCCESS = 0
- #
- # Function Defs.
- # Wheeeeeeeee......
- #
- def build_ssh_command( user = DEVICE_USERNAME, \
- pasw = DEVICE_PASSWORD, \
- addr = DEVICE_ADDRESS ):
- if (not addr):
- raise ValueError("No address specified!")
- elif (user):
- destination = user + '@' + addr
- else:
- destination = addr
- # This could probably be done in a much safer, much more
- # intelligent way, but for now, concat for the win!
- command = SSH_BIN + ' ' + dest
- return command
- def perform_expect_command( expect_child, \
- expect_line = '', \
- response_line = '', \
- timeout = 0 ):
- if ( timeout > 0):
- expect_child.expect(expect_line, timeout=timeout)
- else:
- expect_child.expect(expect_line)
- expect_child.sendline(response_line)
- # Command line parsing
- parser = OptionParser()
- parser.add_option("-H", "--hostname", dest="address")
- parser.add_option("-u", "--username", dest="username")
- parser.add_option("-p", "--password", dest="password")
- parser.add_option("-t", "--tftp-address", dest="tftp-address")
- parser.add_option("-f", "--filename", dest="filename")
- parser.add_option("-d", "--debug", dest="DEBUG", action="store_true")
- parser.add_option("-D", "--no-date", dest="NO_DATE", action="store_true")
- (options, args) = parser.parse_args()
- ####### ssh_proc = pexpect.spawn ( build_ssh_command
- try:
- cmd = build_ssh_command( options['username'], options['password'], options['address'] )
- print cmd
- except ValueError:
- print str(ValueError)
- sys.exit(EXIT_CODE_FAIL)
- #####################
- Error:
- Traceback (most recent call last):
- File "./juniper-tftp-backup.py", line 97, in <module>
- cmd = build_ssh_command( options['username'], options['password'], options['address'] )
- AttributeError: Values instance has no attribute '__getitem__'
Add Comment
Please, Sign In to add comment