import pexpect
import sys
import time
child = pexpect.spawn('scp /root/test.c root@192.168.197.128:/home/')
#child = pexpect.spawn('ssh root@192.168.197.128')
fout = file('mylog.txt','w')
child.logfile = sys.stdout
child.expect("password:")
child.sendline('1')
print "before:"+child.before
print "after:"+child.after
child.expect("$")
time.sleep(2)
参考:python自动化运维pexpect