|
def __init__(self, ip, username, password, port=22,timeout=30): self.ip = ip
self.username = username
self.password = password
self.port = port
self.timeout = timeout
# transport和chanel
self.t = ''
self.chan = ''
# 链接失败的重试次数
self.try_times = 3
# 调用该方法连接远程主机
def connect(self):
transport = paramiko.Transport((self.ip, self.port))
transport.connect(username=self.username, password=self.password)
self.__transport = transport
# 断开连接
def close(self):
self.__transport.close()
# 发送要执行的命令
def send(self, command):
self.connect()
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
result = stdout.read()
print result
self.close()
# get单个文件
def sftp_get(self, remotefile, localfile):
t = paramiko.Transport(sock=(self.ip, self.port))
t.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(remotefile, localfile)
print '下载完成'
t.close()
# put单个文件
def sftp_put(self, localfile, remotefile):
t = paramiko.Transport(sock=(self.ip, self.port))
t.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(localfile, remotefile)
print "上传成功"
t.close()
# ------获取远端linux主机上指定目录及其子目录下的所有文件------
def __get_all_files_in_remote_dir(self, sftp, remote_dir):
# 保存所有文件的列表
all_files = list()
# 去掉路径字符串最后的字符'/',如果有的话
if remote_dir[-1] == '/':
remote_dir = remote_dir[0:-1]
# 获取当前指定目录下的所有目录及文件,包含属性值
files = sftp.listdir_attr(remote_dir)
for x in files:
# remote_dir目录中每一个文件或目录的完整路径
filename = remote_dir + '/' + x.filename
# 如果是目录,则递归处理该目录,这里用到了stat库中的S_ISDIR方法,与linux中的宏的名字完全一致
if S_ISDIR(x.st_mode):
all_files.extend(self.__get_all_files_in_remote_dir(sftp, filename))
else:
all_files.append(filename)
return all_files
# ------获取本地指定目录及其子目录下的所有文件------
def __get_all_files_in_local_dir(self, local_dir):
# 保存所有文件的列表
all_files = list()
# 获取当前指定目录下的所有目录及文件,包含属性值
files = os.listdir(local_dir)
for x in files:
# local_dir目录中每一个文件或目录的完整路径
filename = os.path.join(local_dir, x)
# 如果是目录,则递归处理该目录
if os.path.isdir(x):
all_files.extend(self.__get_all_files_in_local_dir(filename))
else:
all_files.append(filename)
return all_files
#获取本地指定目录及其子目录下的所有文件
def sftp_put_dir(self, local_dir, remote_dir):
t = paramiko.Transport(sock=(self.ip, self.port))
t.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(t)
# 去掉路径字符穿最后的字符'/',如果有的话
if remote_dir[-1] == '/':
remote_dir = remote_dir[0:-1]
# 获取本地指定目录及其子目录下的所有文件
all_files = self.__get_all_files_in_local_dir(local_dir)
# 依次put每一个文件
for x in all_files:
filename = os.path.split(x)[-1]
remote_filename = remote_dir + '/' + filename
print u'Put文件%s传输中...' % filename
sftp.put(x, remote_filename)
# 获取远端linux主机上指定目录及其子目录下的所有文件
def sftp_get_dir(self, remote_dir, local_dir):
t = paramiko.Transport(sock=(self.ip, self.port))
t.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(t)
# 获取远端linux主机上指定目录及其子目录下的所有文件
all_files = self.__get_all_files_in_remote_dir(sftp, remote_dir)
# 依次get每一个文件
for x in all_files:
filename = x.split('/')[-1]
local_filename = os.path.join(local_dir, filename)
print u'Get文件%s传输中...' % filename
sftp.get(x, local_filename)
#文件分离
def copy_file(self, base_file_path):
file_list = os.listdir(base_file_path)
s = set()
for i in file_list: # 取文件名中的年月日,存set去重
data = i.split('_')[:3]
d = ''
for j in data:
d = d + j + "_"
s.add(d)
base_path = os.path.dirname(base_file_path) # 取基础文件的父目录
for i in s:
path = os.path.join(base_path, i) # 拼路径(基础文件的父目录+以年月日命名的文件名)
if not os.path.exists(path):
os.mkdir(r'%s\%s' % (base_path, i)) # 新建文件,以年月日命名
for j in file_list:
if i in j:
new_path = os.path.join(path, j)
file_path = os.path.join(base_file_path, j)
shutil.copyfile(file_path, new_path) # 复制文件
print('复制完成!!') |
|
|