#!/usr/bin/python
# coding=utf-8
import paramiko
import os,time
import configparser
curpath=os.path.realpath("ip.ini")
conf=configparser.ConfigParser()
#conf=configparser.configparser()
conf.read(curpath)
ip_list=conf.get("ip_list","ftpInfo")
user=conf.get("ip_list","user")
psw=conf.get("ip_list","psw")
ftppath_list=conf.get("ip_list","ftppath")
local_path=conf.get("ip_list","dstfolder")
f=conf.get("ip_list","rules")
nun_f=f.split('|')
#print(find_files)
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)
log("host,username:"+host+","+username)
sftp = paramiko.SFTPClient.from_transport(sf)
print("LOGGIN SUCCESS!")
log("loggin sucess!")
log("find file:"+f)
try:
if os.path.isdir(local):#判断本地参数是目录还是文件
for n_f in sftp.listdir(remote):
n_f1=n_f
#log(n_f1)
if n_f.split("=")[2]==f:
for sub_f in sftp.listdir(remote+n_f1+"/"):
print(sub_f)
log(sub_f)
sftp.get(os.path.join(remote+n_f1+"/"+sub_f),os.path.join(local+sub_f))
else:
sftp.get(remote,local)
#except Exception as ex:
except Exception as ex:
print('download exception:', ex)
sf.close()
if __name__ == '__main__':
host=ip_list#主机
#port = 22 #端口
username =user #用户名
password = psw #密码
local = local_path#本地文件或目录,与远程一致,当前为windows目录格式,window目录中间需要使用双斜线
remote =ftppath_list#远程文件或目录,与本地一致,当前为linux目录格式
#sftp_upload(host,port,username,password,local,remote)#上传
sftp_download(host,username,password,local,remote)#下载
*************************************************************************
多文件下载
!/usr/bin/python
# coding=utf-8
import paramiko
import os,time
import configparser
curpath=os.path.realpath("ip.ini")
conf=configparser.ConfigParser()
#conf=configparser.configparser()
conf.read(curpath)
ip_list=conf.get("ip_list","ftpInfo")
user=conf.get("ip_list","user")
psw=conf.get("ip_list","psw")
ftppath_list=conf.get("ip_list","ftppath")
local_path=conf.get("ip_list","dstfolder")
f=conf.get("ip_list","rules")
num_f=f.split('|')
#print(find_files)
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)
log("host,username:"+host+","+username)
sftp = paramiko.SFTPClient.from_transport(sf)
print("LOGGIN SUCCESS!")
log("loggin sucess!")
log("find file:"+f)
try:
if os.path.isdir(local):#判断本地参数是目录还是文件
x=1
while x <= len(num_f):
muli_f = num_f[x - 1]
print("downing..." + muli_f)
log(muli_f)
for n_f in sftp.listdir(remote):
n_f1=n_f
#log(n_f1)
if n_f.split("=")[2]==muli_f:
for sub_f in sftp.listdir(remote+n_f1+"/"):
print(sub_f)
log(sub_f)
sftp.get(os.path.join(remote+n_f1+"/"+sub_f),os.path.join(local+sub_f))
x = x + 1
else:
sftp.get(remote,local)
#except Exception as ex:
except Exception as ex:
print('download exception:', ex)
sf.close()
if __name__ == '__main__':
host=ip_list#主机
#port = 22 #端口
username =user #用户名
password = psw #密码
local = local_path#本地文件或目录,与远程一致,当前为windows目录格式,window目录中间需要使用双斜线
remote =ftppath_list#远程文件或目录,与本地一致,当前为linux目录格式
#sftp_upload(host,port,username,password,local,remote)#上传
sftp_download(host,username,password,local,remote)#下载
PYTHON利用SFTP进行单或多文件下载
最新推荐文章于 2024-07-13 07:09:37 发布