Python expect模块

Expect python也是有支持的,只不过换个名字pexpext而已.

status = pexpect.spawn('ssh %s "%s"' % (host,command)) #此处需要执行的命令
    try:
        i = status.expect(['password:', 'continue connecting (yes/no)?'], timeout=1)
        if i == 0:#不成立输入密码
            status.sendline(passwd)
        elif i == 1:#成立输入yes和密码
            status.sendline('yes')
            status.expect('password: ')
            status.sendline(passwd)
    except pexpect.EOF:
        print "Host -> DOWN"
        print '#'*33
        print "\n"
    except pexpect.TIMEOUT:
        print "Host -> TIMEOUT"
        print '#'*33
        print "\n"
    else:
        data = status.read()
        Pass

    status.close()

你可能感兴趣的:(python)