pexpect不能正常工作

时间:2015-10-29 10:54:23

标签: python python-2.7 pexpect

我正在尝试将一些文件从远程服务器复制到我的机器上。当问到我时,我在pexpect中使用spawn进行身份验证。我部分成功地从服务器下载文件。问题是在完成下载之前抛出异常“ETAException pexpect.ExceptionPexpect:ExceptionPexpect()>忽略”

以下是我的代码:

def doScp(user,password,host,remotepath,localpath,files):
try:
    print files
    child = pexpect.spawn('sudo scp -C %s:%s%s %s' % (host, remotepath, files, localpath))
    child.logfile = sys.stdout
    print 'scp -C %s:%s%s %s' % (host, remotepath, files, localpath)
    i = child.expect(['assword', r"yes/no"], timeout=20)
    if i == 0:
        print "Value of I is Zero\n"
        child.sendline(password)
        j = child.expect(['yes/no'],timeout=20)
        if j == 0:
            child.sendline("yes")
        child.expect(pexpect.EOF, timeout=None)
    elif i == 1:
        child.sendline("yes")
        child.expect("assword", timeout=20)
        child.sendline(password)
        child.expect(pexpect.EOF, timeout=None)
    child.interact()
except pexpect.ExceptionPexpect, e:
    return False

1 个答案:

答案 0 :(得分:1)

我自己找到了答案。问题在于超时。我现在没有给出超时工作正常:)这是我的代码

def doScp(user,password,host,remotepath,localpath,files):
    try:
        print files
        child = pexpect.spawn('sudo scp -C %s:%s%s %s' % (
                              host, remotepath, files, localpath))
        child.logfile = sys.stdout
        print 'scp -C %s:%s%s %s' % (host, remotepath, files, localpath)
        i = child.expect(['assword', r"yes/no"], timeout=None)
        if i == 0:
            print "Value of I is Zero\n"
            child.sendline(password)
            j = child.expect(['yes/no'],timeout=None)
            if j == 0:
                child.sendline("yes")
            child.expect(pexpect.EOF, timeout=None)
        elif i == 1:
            child.sendline("yes")
            child.expect("assword", timeout=None)
            child.sendline(password)
            child.expect(pexpect.EOF, timeout=None)
        child.interact()
    except pexpect.ExceptionPexpect, e:
        return False