python之pexpect用法及scp新用途

 import pexpect

def scp_cmd():
    passwd='*******'
    passwd1='*******'
    ssh = pexpect.spawn('scp -rp [email protected]:/backup [email protected]:/data')
    r = ''
    try:
        i = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
        if i == 0 :
            ssh.sendline(passwd)
        elif i == 1:
            ssh.sendline('yes')
            ssh.expect('password:')
            ssh.sendline(passwd)
        b=ssh.expect(['password: ','continue connecting (yes/no)?'])
        if b==0:
            ssh.sendline(passwd1)
        elif b==1:
            ssh.sendline('yes')
            ssh.expect('password:')
            ssh.sendline(passwd1)
    except pexpect.EOF:
        ssh.close()
    else:
        r = ssh.read()
        ssh.expect(pexpect.EOF)
        ssh.close()
    return r
scp_cmd()

你可能感兴趣的:(python,pexpect)