1、sftp批量提取(绝对好用)
#!/usr/bin/python
# coding=utf-8
import paramiko
import os, time, sys
import configparser
default_encodeing = 'gbk'
curpath = os.path.realpath("downloadmr.ini")
conf = configparser.ConfigParser()
conf.read(curpath)
ip_list = conf.get("ip_list", "ftpInfo")
ip_num = ip_list.split("|")
ip_nums = len(ip_num)
FTP_PATH = conf.get("ip_list", "ftppath")
local_path = conf.get("ip_list", "dstfolder")
find_file=conf.get("ip_list","rules")
find_files=find_file.split('|')
#print(find_files)
ftype=conf.get("ip_list","filetype")
st=conf.get("ip_list","s")
ed=conf.get("ip_list","e")
def log(s):
T = time.strftime("%Y%m%d-%H:%M:%S", time.localtime())
print(T + '\t' + s)
with open("log.txt", "a") as fp:
fp.write(T + '\t' + s + '\n')
def sftp_download(host, username, password, local, remote):
sf = paramiko.Transport((host))
sf.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local): # 判断本地参数是目录还是文件
for ndocum in sftp.listdir(remote):
#print(ndocum)
if ndocum >= st and ndocum <= ed:
newpath=os.path.join(FTP_PATH,ndocum)
for n_files in sftp.listdir(newpath):
x= 0
while x < len(find_files):
f = find_files[x]
if n_files.find(f,1)>0 and n_files.find(ftype)>0:
#if os.path.split(ndocum)[-1] == f:
log("find files and downloading...:"+ip+n_files)
#lastpath=os.path.join(newpath, n_files)
log(ip+":"+newpath+"/"+n_files)
sftp.get(newpath+"/"+n_files, os.path.join(local + n_files))
log(ip+":"+n_files+" files is done!")
print(ip+":"+n_files+ " are fished!")
x = x + 1
else:
x=x+1
else:
sftp.get(remote, local)
except Exception as ex:
print('download exception:', ex)
log("download files to " + host + " failed!")
sf.close()
if __name__ == '__main__':
n = 0
while n < ip_nums:
ip_new = ip_num[n]
ip =ip_new.split(',')[0]
UserName = ip_new.split(',')[1]
Password = ip_new.split(',')[2]
host = ip # 主机
# port = 22 #端口
username = UserName # 用户名
password = Password # 密码
local = local_path # 本地文件或目录,与远程一致,当前为windows目录格式,window目录中间需要使用双斜线
remote = FTP_PATH
#res = sftp_download(host, username, password, local, remote)
sftp_download(host, username, password, local, remote) # 下载
n=n+1
2、SFTP批量上传
#!/usr/bin/python
# coding=utf-8
import paramiko
import os,time,sys
import configparser
default_encodeing = 'gbk'
curpath=os.path.realpath("ftp_ul.ini")
conf=configparser.ConfigParser()
conf.read(curpath)
ip_list=conf.get("ip_list","ftpInfo")
ip_num=ip_list.split("|")
ip_nums=len(ip_num)
FTP_PATH=conf.get("ip_list","ftppath")
local_path=conf.get("ip_list","dstfolder")
def log(s):
T = time.strftime("%Y%m%d-%H:%M:%S", time.localtime())
print(T + '\t' + s)
with open("log.txt", "a") as fp:
fp.write(T + '\t' + s + '\n')
def sftp_upload(host,username,password,local,remote,timeout=10):
sf = paramiko.Transport((host))
sf.connect(username = username,password = password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local):#判断本地参数是目录还是文件
for f in os.listdir(local):#遍历本地目录
sftp.put(os.path.join(local+f),os.path.join(remote+f))#上传目录中的文件
log("upload files to server:"+host+":"+f)
else:
sftp.put(local,remote)#上传文件
except Exception as ex:
print('download exception:', ex)
log("upload files to "+host+" failed!")
sf.close()
if __name__ == '__main__':
n = 0
while n < ip_nums:
ip_new = ip_num[n]
# print(ip_new)
ip = ip_new.split(',')[0]
UserName = ip_new.split(',')[1]
Password = ip_new.split(',')[2]
host = ip # 主机
# port = 22 #端口
username = UserName # 用户名
password = Password # 密码
local = local_path # 本地文件或目录,与远程一致,当前为windows目录格式,window目录中间需要使用双斜线
remote = FTP_PATH
res=sftp_upload(host,username,password,local,remote)
# if not res:
# print("skin...")
# n = n + 1
#continue
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)