Python paramiko模块基本使用(一)

Posted 那年花开月正圆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python paramiko模块基本使用(一)相关的知识,希望对你有一定的参考价值。

使用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模块基本使用(一)的主要内容,如果未能解决你的问题,请参考以下文章

Paramiko和MySQL相关-day10

Paramiko模块,用python代码连接服务器执行命令

python第三方模块之paramiko模块

如何进行服务器的批量管理以及python 的paramiko的模块

python之paramiko模块

Python3之paramiko模块