python脚本可以继续出错吗?

时间:2014-01-20 11:46:14

标签: python

如果我的列表中的主机没有响应ssh,脚本就会死掉。我希望它继续列在我的列表中,然后告诉我"Error Connecting to X.X.X.X"。这样做的最佳方式是什么?我想在某些地方应该使用Exception,但我不知道在哪里或如何。

我知道错误只是说“我期待”child.sendline(cmd)“中的某个属性,但是没有收到”

[-] Error Connecting to 10.26.0.1
Traceback (most recent call last):
  File "asaos-snmpv3-tool.py", line 179, in <module>
   main()
  File "asaos-snmpv3-tool.py", line 160, in main
   send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
  File "asaos-snmpv3-tool.py", line 48, in send_command
child.sendline(cmd)
AttributeError: 'NoneType' object has no attribute 'sendline'


def send_command(child, cmd):
    child.sendline(cmd)
    child.expect(PROMPT)
    print child.before

def connect(user, host, passwd, en_passwd):
    ssh_newkey = 'Are you sure you want to continue connecting?'
    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, '[P|p]assword:'])
        if ret == 0:
            print '[-] Could not accept new key from ' + host
            return
    child.sendline(passwd)
    child.expect(PROMPT)
    child.sendline('enable')
    child.sendline(en_passwd)
    child.expect(PROMPT)
    child.sendline('config t')
    child.expect(PROMPT)
    return child


def main():
    parser = argparse.ArgumentParser('--host --host_file --username --password--enable --group --snmp_user --snmp_host\
    --snmp_contact --int_name --snmp_v3_auth --snmp_v3_hmac --snmp_v3_priv --snmp_v3_encr')
    parser.add_argument('--host_file', dest='hosts', type=file, help='specify a target host file')

hosts = args.hosts

if hosts:
    for line in hosts:
        host = line.rstrip()
        child = connect(user, host, passwd, en_passwd)
        send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
        send_command(child, SNMPSRVUSRCMD + snmpuser + ' ' + group + V3AUTHCMD + SHAHMACCMD + snmpauth + PRIVCMD +
                            snmpencrypt + ' ' + snmppriv)
        send_command(child, SNMPSRVHOSTCMD + intname + ' ' + snmphost + VERSION3CMD + snmpuser)
        send_command(child, SNMPSRVCONTACTCMD + snmpcontact)
        send_command(child, SNMPSRVENTRAP)
        send_command(child, WRME)


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

根据您现有的代码:您已经有了这种机制,如果connect无法连接,则返回None,所以:

if hosts:
    for line in hosts:
        host = line.rstrip()
        child = connect(user, host, passwd, en_passwd)
        if child:
            send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
            send_command(child, SNMPSRVUSRCMD + snmpuser + ' ' + group + V3AUTHCMD + SHAHMACCMD + snmpauth + PRIVCMD +
                            snmpencrypt + ' ' + snmppriv)
            send_command(child, SNMPSRVHOSTCMD + intname + ' ' + snmphost + VERSION3CMD + snmpuser)
            send_command(child, SNMPSRVCONTACTCMD + snmpcontact)
            send_command(child, SNMPSRVENTRAP)
            send_command(child, WRME)

如果您确实想要使用异常,则需要让connect函数在失败时引发一个,请查看注释中的链接。