标签:client def readline batch shc exec tab ssi python
使用paramiko模块登录远程主机,对日志进行统计分析。
import paramiko def batch_count(days, hours, ips, user, passwd, source_path, dest_path, port=22): for ip in ips: count(days, hours, ip, user, passwd, source_path, dest_path, port=22) def count(days, hours, ip, username, password,source_path, dest_path, port=22): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,port,user,passwd) num = [] for day in days: cmd1 = "grep \"%s/May/2018:\" %s/access.log | grep \"404\" > %s/day%s.log" %(day, source_path, dest_path, day) print cmd1 stdin, stdout, stderr = ssh.exec_command(cmd1) tmp = [] for hour in hours: cmd2 = "grep \"%s/May/2018:%s\" %s/day%s.log |wc -l" %(day, hour, dest_path, day) print cmd2 stdin, stdout, stderr = ssh.exec_command(cmd2) out = stdout.readline().strip() tmp.append(out) num.append(tmp) draw(num, days, hours, ip) output(num, days, hours, ip) def draw(table, days, hours, ip): print ip print ‘ ‘, for i in hours: print ‘%6s‘ %i , print ‘%6s‘ %‘sum‘ , print ‘‘ for i in range(len(days)): print ‘D%s‘ % days[i] , sum = 0 for j in range(len(hours)): sum = sum + int(table[i][j].encode("utf-8")) print ‘%6s‘ % table[i][j] , print ‘%6d‘ % sum, print "" def output(table,days,hours,ip): with open(‘./count.log‘,‘aw‘) as f: f.write(‘%s\n‘ % ip) f.write(‘%5s‘ % ‘‘) for i in hours: f.write(‘%6s‘ % i) f.write(‘%6s‘ % ‘sum‘) f.write("\n") for i in range(len(days)): f.write(‘day%s‘ % days[i]) sum = 0 for j in range(len(hours)): sum = sum + int(table[i][j].encode("utf-8")) f.write(‘%6s‘ % table[i][j]) f.write(‘%6d‘ % sum) f.write("\n") if __name__ == ‘__main__‘: days = [‘04‘,‘05‘,‘06‘] hours = [‘10‘,‘11‘,‘12‘,‘13‘,‘14‘,‘15‘,‘16‘,‘17‘,‘18‘] ips = [‘132.121.89.65‘,‘132.121.89.66‘] user = ‘opan‘ passwd = ‘1*p%N0z9‘ source_path = ‘/data1/logs/nginx‘ dest_path = ‘/home/opan‘ batch_count(days, hours, ips, user, passwd, source_path, dest_path, port=22)
标签:client def readline batch shc exec tab ssi python
原文地址:https://www.cnblogs.com/prometheus-python-xshell/p/9070892.html