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 raise 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

相關內容