When I run this script on both ASAOS and IOS devices it find that they are different in the enable_mode function, and issues 'terminal pager 0' for ASAOS, and 'terminal length 0' for IOS.
Then in the main function I have a 'show run' command issued. This command is issued on the IOS device, but it seems to stop right after the 'terminal pager 0' command is issued on the ASA.
Is it safe to assume that the only output to the console (other then 'print') is the last successful string?
Here is what I get when I run the script
terminal pager 0
home-as <= name of the ASA minus the last letter for some reason
show run <= show run command issued on the IOS switch
Building configuration...
[EXTRACTED]
def enable_mode(user, host, passwd, en_passwd):
ssh_newkey = 'Are you sure you want to continue connecting (yes/no)?'
constr = 'ssh ' + user + '@' + host
child = pexpect.spawn(constr)
ret = child.expect([pexpect.TIMEOUT, ssh_newkey, '[P|p]assword:'])
if ret == 0:
print '[-] Error Connecting to ' + host
return
if ret == 1:
child.sendline('yes')
ret = child.expect([pexpect.TIMEOUT])
if ret == 0:
print '[-] Could not accept new key from ' + host
return
child.sendline(passwd)
auth = child.expect(['[P|p]assword:', '.>', '.#'])
if auth == 0:
print 'User password is incorrect'
return
if auth == 1:
child.sendline('enable')
child.sendline(en_passwd)
enable = child.expect([pexpect.TIMEOUT, '.#'])
if enable == 0:
print 'enable password for ' + host + ' is incorrect'
return
if enable == 1:
child.sendline(SHOWVER)
what_os = child.expect([pexpect.TIMEOUT, '.IOS.', '.Adaptive.'])
if what_os == 0:
print 'show ver' + ' time out' + 'for ' + host
return
if what_os == 1: # IOS
child.sendcontrol('c')
child.expect(PRIV_EXEC_MODE)
child.sendline(IOSTERMLEN0)
child.expect(PRIV_EXEC_MODE)
return child
if what_os == 2: # ASAOS
child.sendline(QOUTMORE)
child.expect(PRIV_EXEC_MODE)
child.sendline(ASATERMPAGER0)
child.expect(PRIV_EXEC_MODE)
return child
def main():
conf_parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False)
conf_parser.add_argument('--conf_file', help='Specify a conf file', metavar='FILE')
conf_parser.add_argument('--host_file', dest='hosts', type=file, help='specify a target host file')
args, remanaing_argv = conf_parser.parse_known_args()
if args.conf_file:
conf = ConfigParser.SafeConfigParser()
conf.read([args.conf_file])
defaults = dict(conf.items('Defaults'))
else:
defaults = {'option': 'default'}
parser = argparse.ArgumentParser(parents=[conf_parser])
parser.set_defaults(**defaults)
args = parser.parse_args()
hosts = args.hosts
user = args.user
passwd = args.passwd
en_passwd = args.en_passwd
if hosts:
for line in hosts:
host = line.rstrip()
child = enable_mode(user, host, passwd, en_passwd)
if child:
current_time = time.strftime('%m.%d.%y.%M.%S', time.localtime())
output_name = "cisco_configs/{0}_{1}.txt".format(host, current_time)
sys.stdout = open(output_name, 'w')
send_command(child, SHOWRUN)
else:
print('I need hosts!!')
if __name__ == '__main__':
main()
Got it working. I was using a variable "QOUTMORE" under "enable_mode" and "if what_os == 2". This variable was set to 'q' in another module called with "from modules.cmds import *", but instead I used a 'q' and not the variable in my "enable_mode" function. Not sure why its working but it is.