1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| import paramiko import _thread import os import threading import time
threadNames = ['1号服务器', '2号服务器', '3号服务器'] nowSum = {} totalSum = {} for threadname in threadNames: nowSum[threadname] = 0 totalSum[threadname] = 0
def callback(now, total, length=30, prefix='进度:', thread_name='', number=0): nowSum[threading.current_thread().getName()] = now totalSum[threading.current_thread().getName()] = total print( '\r文件数量:3, ' + '文件总大小:{}, '.format(str(format(sum(totalSum.values()) / 1024 / 1024, '.2f')) + 'M') + prefix + '{:.2%}\t'.format((sum(nowSum.values()) / sum(totalSum.values()))) + '[' + '>' * int(sum(nowSum.values()) / sum(totalSum.values()) * length) + '-' * int(length - sum(nowSum.values()) / sum(totalSum.values()) * length) + ']', end='')
dateStr = input('请输入日期,格式为yyyy-mm-dd(默认今天):') grep_str = input('请输入查询内容(默认全部):')
today = time.strftime('%Y-%m-%d', time.localtime(time.time())) filename = 'MyProject.log' if not dateStr: dateStr = today if today != dateStr: filename = 'MyProject.log_' + dateStr + '.log'
server1 = 'xxx.xxx.xxx.xx' server1port = 22 server1username = 'xx' server1password = 'xxxxxx' server1remotefilepath = '/usr/local/....' server1localfilepath = 'D:\\xxx\\log\\' + dateStr + '\\'+ grep_str +'$1\\' server2 = 'xxx.xxx.xxx.xx' server2port = 22 server2username = 'xxxxxx' server2password = 'xxx' server2remotefilepath = '/usr/local/....' server2localfilepath = 'D:\\xxx\\log\\' + dateStr + '\\'+ grep_str +'$2\\' server2remotefilepath2 = '/usr/local/....' server2localfilepath2 = 'D:\\work\\log\\' + dateStr + '\\'+ grep_str +'$3\\'
def getFileDirPath(filepath): print(filepath[0:filepath.rfind('\\')])
def getFileName(filepath): print(filepath[filepath.rfind('\\'):])
def downloadLog(thread_name, host, port, username, password, remote_filepath, local_filepath): tranport = paramiko.Transport((host, port)) tranport.connect(None, username, password) sftp = paramiko.SFTPClient.from_transport(tranport) sftp.get(remote_filepath, local_filepath, callback=callback) if sftp: sftp.close() if tranport: tranport.close() print(local_filepath + ': 下载完成', end='\n') def downloadGrepLog(thread_name, host, port, username, password, remote_filepath, local_filepath, grep_str): command = 'grep -A 20 -B 2 "' + grep_str + '" ' + remote_filepath s = paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host, port, username, password) print('grep命令' + command) (stdin, stdout, stderr) = s.exec_command(command) f = open(local_filepath,"w+") for line in stdout: f.write(line) s.close() print(local_filepath + ': 下载完成', end='\n')
class downloadThread(threading.Thread): def __init__(self, name, host, port, username, password, remote_filepath, local_filepath, grep_str): threading.Thread.__init__(self) self.name = name self.host = host self.port = port self.username = username self.password = password self.remote_filepath = remote_filepath self.local_filepath = local_filepath self.grep_str = grep_str def run(self): if not self.grep_str: downloadLog(self.name, self.host, self.port, self.username, self.password, self.remote_filepath, self.local_filepath) else: downloadGrepLog(self.name, self.host, self.port, self.username, self.password, self.remote_filepath, self.local_filepath,self.grep_str)
if not os.path.exists(server1localfilepath): os.makedirs(server1localfilepath) if not os.path.exists(server2localfilepath): os.makedirs(server2localfilepath) if not os.path.exists(server2localfilepath2): os.makedirs(server2localfilepath2)
thread1 = downloadThread('1号服务器', server1, server1port, server1username, server1password, server1remotefilepath + filename, server1localfilepath + filename, grep_str) thread2 = downloadThread('2号服务器-1', server2, server2port, server2username, server2password, server2remotefilepath + filename, server2localfilepath + filename, grep_str) thread3 = downloadThread('2号服务器', server2, server2port, server2username, server2password, server2remotefilepath2 + filename, server2localfilepath2 + filename, grep_str)
thread1.setDaemon(True) thread1.start()
thread2.setDaemon(True) thread2.start()
thread3.setDaemon(True) thread3.start()
thread1.join() thread2.join() thread3.join()
print('主线程结束:')
|