SCP没有在Pexpect工作


0

我正在尝试做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))


文件“/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))
Rajesh 2015年

Answers:


0

试试这样:

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

这个链接可以帮助你:

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


我收到以下错误文件“/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))
Rajesh 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.