为什么我的孩子没有回来

时间:2014-02-11 15:54:02

标签: python pexpect

当我在ASAOS和IOS设备上运行此脚本时,它发现它们在enable_mode函数中有所不同,并为ASAOS发出'terminal pager 0',为IOS发出'terminal length 0'。

然后在main函数中我发出了'show run'命令。此命令在IOS设备上发出,但似乎在ASA上发出'terminal pager 0'命令后立即停止。

是否可以安全地假设控制台的唯一输出(除了'print'之外)是最后一个成功的字符串?

这是我运行脚本时得到的结果

终端寻呼机0

home-as< =由于某种原因,ASA的名称减去最后一个字母

在IOS开关上显示run run< = show run命令

构建配置......

[浸提]

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()

1 个答案:

答案 0 :(得分:0)

搞定了。我在“enable_mode”和“if what_os == 2”下使用变量“QOUTMORE”。在使用“from modules.cmds import *”调用的另一个模块中,此变量设置为“q”,但我在“enable_mode”函数中使用了“q”而不是变量。不确定为什么它的工作但是它。