Pexpect에서 SCP가 작동하지 않습니다

Pexpect에서 SCP가 작동하지 않습니다

SCP를 수행하고 원격 서버에서 일부 파일을 복사하려고 합니다. 루트 권한이 없기 때문에 비밀번호를 묻는 메시지가 표시될 때 명령과 함께 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 return 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

관련 정보