3rd Party Library
Pexpect
http://stackoverflow.com/questions/20430119/send-password-when-using-scp-to-copy-files-from-one-server-to-another
Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes’ Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands.
Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing.
def doScp(user,password, host, path, files):
fNames = ' '.join(files)
print fNames
child = pexpect.spawn('scp %s %s@%s:%s' % (fNames, user, host,path))
print 'scp %s %s@%s:%s' % (fNames, user, host,path)
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()
Linux expect:
http://www.thegeekstuff.com/2010/10/expect-examples/
#!/usr/bin/expect -f
# connect via scp
spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "PASSWORD\r"
}
}
interact
网友评论