python sftp.get 同步远程文件夹 实践二
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python sftp.get 同步远程文件夹 实践二相关的知识,希望对你有一定的参考价值。
主要用途,从本机获取远端机器的文件夹
#!/usr/bin/python import pexpect import paramiko import os import sys import time import multiprocessing import datetime import crash_on_ipy from stat import S_ISDIR ip_list = [] #room_id = sys.argv[1] cur_time = datetime.datetime.now() class get_thread(): def __init__(self,host_ip=‘ip‘): self.host_ip=‘ip‘ self.scp = paramiko.SSHClient() self.scp.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.scp = paramiko.Transport((host_ip, 22)) self.scp.connect(username=‘tomcat‘, password=‘xx‘) self.sftp = paramiko.SFTPClient.from_transport(self.scp) def _walk_remote(self, dirpath): dirnames = [] filenames = [] for fd in self.sftp.listdir_attr(dirpath): if S_ISDIR(fd.st_mode): dirnames.append(fd.filename) else: filenames.append(fd.filename) yield dirpath, dirnames, filenames for dirname in dirnames: new_dirpath = os.path.join(dirpath, dirname) # yield from self._walk_remote(new_dirpath) for walk in self._walk_remote(new_dirpath): yield walk def getall(self,local,remote): st_mode = self.sftp.stat(remote).st_mode if not S_ISDIR(st_mode): filename = os.path.basename(remote) self.sftp.get(remote, os.path.join(local, filename)) else: parent, child = os.path.split(remote) for dirpath, dirnames, filenames in self._walk_remote(remote): dirpath = dirpath.replace(parent, ‘.‘) for dirname in dirnames: os.makedirs(os.path.join(local, dirpath, dirname)) for filename in filenames: localpath = os.path.join(local, dirpath, filename) remotepath = os.path.join(parent, dirpath, filename) self.sftp.get(remotepath, localpath) self.scp.close() if __name__==‘__main__‘: print "-"*50 getthread=get_thread() room_pathd = ‘/opt/src/logs/‘ room_paths = ‘/home/python/‘ getthread.getall(room_paths,room_pathd)
有点问题,两次运行会报错,需要修改
本文出自 “人,要有自己的想法” 博客,请务必保留此出处http://szgb2016.blog.51cto.com/340201/1860408
以上是关于python sftp.get 同步远程文件夹 实践二的主要内容,如果未能解决你的问题,请参考以下文章
ssh2-sftp-client get()请求提供“拒绝权限-错误”