源码如下:
import paramiko
import os
import re
import zipfile
import tarfile
from scp import SCPClient
import configparser
# 获取配置文件数据
def get_cfg_data(cfg_path):
cf = configparser.ConfigParser()
cf.read(cfg_path, encoding='utf-8')
# 获取所有section,返回值为list
secs = cf.sections()
print('可输入信息列表%s' % secs)
for index, sec in enumerate(secs):
if index == 0:
continue
print(r'输入%s则下载%s项目' % (index, sec))
# # 获取db中的所有属性名
# dboption = cf.options('db')
# print(dboption)
#
# 获取db中的键值对
input_comm = input("根据上面的项目序号输入相应的数字,请输入:")
if secs[int(input_comm)] not in secs:
print('输入的信息有误系统自动退出!')
exit(0)
dbitem = cf.items(secs[int(input_comm)])
dbitem1 = cf.items(secs[0])
print(dict(dbitem), dict(dbitem1))
return dict(dbitem), dict(dbitem1)
def remote_scp(host_ip, port, remote_path, local_path, username, password):
# 记录日志写到本地文件中
paramiko.util.log_to_file(r'./log.txt')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host_ip, port, username, password)
# print("ls %s" % remote_path)
(stdin, stdout, stderr) = ssh.exec_command("ls -rt %s" % remote_path)
versions = stdout.readlines()
version = versions[-1]
version = version.strip()
print('此项目的当前最新版本为:%s' % version)
(stdin, stdout, stderr) = ssh.exec_command("ls -ltrR %s" % remote_path) # 获取所有版本列表
st = stdout.readlines()
mark = 0
dir_list = []
file_list = []
for s in st:
if version in s.strip('\n') and s.strip('\n')[-1:] == ':':
mark += 1
dir_list.append(s.strip('\n'))
print(s.strip('\n'))
if mark > 0 and s.strip('\n')[:1] == '-':
if len(dir_list) > 0 and dir_list[-1] not in file_list:
file_list.append([])
file_list[len(file_list)-1].append(dir_list[-1])
dir_list = []
date_all = re.findall(r"\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}\s(.+)", s.strip('\n'))
for item in date_all:
print(item)
file_list[len(file_list)-1].append(item)
print(s.strip('\n'))
print(file_list)
scpclient = SCPClient(ssh.get_transport(), socket_timeout=150.0)
for file in file_list:
for index, f in enumerate(file):
if index == 0:
continue
src = file[0][:-1]+'/'+f
des = local_path
src_list = src.split('/')
if not os.path.exists(src_list[-2]):
os.makedirs(src_list[-2])
print(src, des)
scpclient.get(src, des+'\\'+src_list[-2])
print(src_list[-1][-3:])
if 'zip' in src_list[-1][-3:].strip(''):
# zipfile解压
print(des+src_list[-2]+'\\'+src_list[-1])
z = zipfile.ZipFile(des+src_list[-2]+'\\'+src_list[-1], 'r')
z.extractall(path=des+src_list[-2])
z.close()
elif 'tar' in src_list[-1][-6:]:
# 解压
tar = tarfile.open(des+'\\'+src_list[-2]+'\\'+src_list[-1], 'r')
tar.extractall(path=des+src_list[-2]) # 可设置解压地址
tar.close()
ssh.close()
if __name__ == '__main__':
# 获取配置文件中的数据
cfg_data, cfg_data1 = get_cfg_data('ftp_project.ini')
host_ip = cfg_data1['host_ip']
remote_path = cfg_data['remote_path']
local_path = cfg_data['local_path']
# part_path = os.path.dirname(local_path)
if not os.path.exists(local_path):
os.makedirs(local_path)
username = cfg_data1['host_user']
password = cfg_data1['host_pw']
remote_scp(host_ip, 22, remote_path, local_path, username, password)