python+paramiko应用示例:下载批量下载日志文件
import os
import threading
import json
import time
import paramiko
"""
服务器的日志/var/log下的日志文件的下载:
1.把清单形成json文件存储在本地
2.系统启用的时候,调用这个配置文件,读取其中的字典项内容
"""
#windows本地存储日志文件的路径
LOCALPATH=os.getcwd()+"\Log"
#服务器列表清单"file_nameList": ["secure","messages","cron"],这个都是/var/log下的文件,如果下载这个文件就是添加到这个列表中
devicelist=[
{"host_ip": "192.168.170", "file_nameList": ["secure","messages","cron"], "remote_path": "/var/log", "local_path": LOCALPATH, "username": "root", "password": "ipss11223344@li"},
{"host_ip": "192.168.1170", "file_nameList": ["secure","messages"], "remote_path": "/var/log", "local_path": LOCALPATH, "username": "root", "password": "ipss11223344@78"},
{"host_ip": "192.168.112", "file_nameList":["secure","messages"], "remote_path": "/var/log", "local_path": LOCALPATH, "username": "root", "password": "ipss11223344@2021@098"},
{"host_ip": "192.168.156", "file_nameList": ["secure","messages"], "remote_path": "/var/log", "local_path": LOCALPATH, "username": "root", "password": "ipss11223344@2021@ert"},
{"host_ip": "192.168.11156", "file_nameList": ["secure","messages"], "remote_path": "/var/log", "local_path": LOCALPATH, "username": "root", "password": "ipss11223344@2021@hjl"},
]
# #写入文件内容,到本地的db.json文件
# with open("./dbconfig/db.json","w",encoding="utf-8")as fjson:
# json.dump(devicelist,fjson)
#这两个文件是/var/log下的文件 "secure","messages"
# file_nameList 作为一个列表里面存储内容如下file_nameList=["secure","messages"]
def remote_scp(host_ip,file_nameList,remote_path, local_path, username, password):
#try的作用就是结束有问题的ip地址或者密码,同时结束这个线程
try:
t = paramiko.Transport((host_ip, 22))
t.connect(username=username, password=password) # 登录远程服务器
sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议
#遍历要下载的日志文件的列表,判断有几个日志文件,分别取出每个日志文件,然后进行下载
for file_name in file_nameList:
src = remote_path + '/' + file_name #服务器log文件的位置
# des = local_path + '/' + file_name
des = local_path + '/' + host_ip+file_name #组合你要保存的日志问价的组成
sftp.get(src, des) #下载文件
t.close() #循环关闭
except:
print(host_ip,"获取日志文件失败!")
# print("获取日志文件失败,请检查账户,密码等基础设置")
time.sleep(1)
return
#思路:
# 1.把配置文件存放到一个大的列表中,列表中每项都是一个字典项目
if __name__ == '__main__':
#从列表中获取每个字典项
for dicitem in devicelist:
host_ip=dicitem['host_ip'] #获取字典项的中的各个字段的信息
file_nameList=dicitem['file_nameList']
remote_path=dicitem['remote_path']
local_path=dicitem['local_path']
username=dicitem['username']
password=dicitem['password']
#多线程启用,调用多个线程同时进行下载日志文件
print(host_ip + "log下载开始!")
th = threading.Thread(target=remote_scp, args=(host_ip, file_nameList, remote_path, local_path, username, password))
th.start()
暑期编程PK赛
得CSDN机械键盘等精美礼品!


