主要用途,从本机获取远端机器的文件夹
#!/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)
有点问题,两次运行会报错,需要修改