嘿,丹尼,试着跟着斯内普。注意:键值来自配置yml是RSA私钥文件的完整路径(如:/home/user/.ssh/id_RSA)。在import paramiko
import StringIO
import yaml
try:
cfg=yaml.load(open("config.yml"))["local"]
paramiko.util.log_to_file('sftpin.log')
sftp = paramiko.Transport(cfg["host"], cfg["port"])
key_string = cfg["key"]
private_key = None
if key_string is not None:
f = open(key_string, 'r')
s = f.read()
not_really_a_file = StringIO.StringIO(s)
private_key = paramiko.RSAKey.from_private_key(not_really_a_file,
password=cfg["key_passwd"])
sftp.connect(username=cfg["user"],
password=cfg["pass"],
pkey=private_key)
sftp = paramiko.SFTPClient.from_transport(sftp)
print 'Connecting'
files = sftp.listdir(cfg["remote_outbox"])
print 'Listing Contents'
print files
print 'Getting Files'
for f in files:
remote = "%s%s" %(cfg["remote_outbox"], f)
local = "%s%s" %(cfg["local_inbox"], f)
try:
sftp.get(remote, local)
except IOError:
print "Failed to copy %s" % remote
print 'Job Completed'
finally:
sftp.close()
敬KG。在