SCP funktioniert nicht in Pexpect

SCP funktioniert nicht in Pexpect

Ich versuche, SCP auszuführen und einige Dateien vom Remote-Server zu kopieren. Da ich keine Root-Berechtigung habe, verwende ich sudo mit dem Befehl. Wenn ich nach einem Passwort gefragt werde, sende ich es mit pexpect, aber ich kann es nicht tun. Ich bin irgendwo hängen geblieben.

Hier ist mein Code:

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)

Der Fehler, den ich bekomme:

Datei „/usr/lib/python2.6/site-packages/pexpect.py“, Zeile 1325, in expect_list return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize) Datei „/usr/lib/python2.6/site-packages/pexpect.py“, Zeile 1409, in expect_loop raise TIMEOUT (str(e) + '\n' + str(self))

Antwort1

Versuchen Sie es folgendermaßen:

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

Vielleicht hilft Ihnen dieser Link:

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

verwandte Informationen