#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pexpect
import time
import traceback
def doRsync(user,passwd,ip,srcDir,dstDir,timeout=3600):
cmd = "rsync -a {srcDir} {rUser}@{rHost}:{dstDir}".format(
rUser = user,rHost=ip,srcDir=srcDir,dstDir=dstDir
)
try:
ssh = pexpect.spawn(cmd,timeout=timeout)
print cmd
i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)
if i == 0 :
ssh.sendline(passwd)
elif i == 1:
ssh.sendline('yes')
ssh.expect('password: ')
ssh.sendline(passwd)
ssh.read()
ssh.close()
except :
#print traceback.format_exc()
pass
if __name__ == '__main__':
doRsync("root","123456","192.168.1.101","/tmp/rtest1","/tmp")