SCP 在 Pexpect 中不起作用

SCP 在 Pexpect 中不起作用

我正在尝试执行 SCP 并从远程服务器复制一些文件。由于我没有 root 权限,因此当它提示输入密码时,我使用 sudo 执行命令,我使用 pexpect 发送密码,但我无法执行此操作。我被困在某个地方。

这是我的代码:

import pexpect

def doScp(user,password,host,remotepath,localpath,files):
    print files

    child = pexpect.spawn('sudo scp -C %s:%s%s %s' % (host, remotepath, files, localpath))

    print 'scp -C %s:%s%s %s' % (host, remotepath, files, localpath)

    i = child.expect(['assword:', r"yes/no"], timeout=30)

    if i == 0:
        child.sendline(password)
    elif i == 1:
        child.sendline("yes")
        child.expect("assword:", timeout=30)
        child.sendline(password)
    data = child.read()
    print data
    child.close()

user = "xxxxx"

host = "yyyy"

password = "zzzzzz"
remotepath = "/opt/logs/"
localpath = "/opt/Performance_Logs/SRNG/"
files = "receiver.log"

doScp(user,password,host,remotepath,localpath,files)

我收到的错误:

文件“/usr/lib/python2.6/site-packages/pexpect.py”,第 1325 行,在 expect_list 中返回 self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize) 文件“/usr/lib/python2.6/site-packages/pexpect.py”,第 1409 行,在 expect_loop 中引发 TIMEOUT (str(e) + '\n' + str(self))

答案1

尝试这样:

child.expect("ada@ada's password:")
child.sendline("mypassword")
child.expect(pexpect.EOF, timeout=10)

此链接可能对您有帮助:

https://github.com/pexpect/pexpect/issues/105

相关内容