码迷,mamicode.com
首页 > 编程语言 > 详细

Python paramiko模块基本使用(一)

时间:2018-05-22 12:50:26      阅读:249      评论:0      收藏:0      [点我收藏+]

标签: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)

 

Python paramiko模块基本使用(一)

标签:client   def   readline   batch   shc   exec   tab   ssi   python   

原文地址:https://www.cnblogs.com/prometheus-python-xshell/p/9070892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!