python 远程统计文件

#!/usr/bin/python

#encoding=utf-8

import time

import os

import paramiko

import multiprocessing



#统计文件数量

def get_total(ip,password,filepath):

    paramiko.util.log_to_file('paramiko.log')

    ssh=paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:

        starttime=time.time()

        ssh.connect(hostname=ip,port=22,username='root',password=password)

        #stdin,stdout,stderr = ssh.exec_command(str(len(os.listdir(filepath))))

        stdin,stdout,stderr = ssh.exec_command('cd filepath ;ls |wc -l')

        #print ip,filepath,stdout.read().strip('\n')

        count=int(stdout.read().strip('\n'))

        endtime=time.time()

        caltime=endtime-starttime

        result=ip+','+filepath.strip('\n')+','+str(count)+','+str(caltime)+'\n'

        return result

    except:

        result=ip+','+filepath.strip('\n')+','+'failed'+'\n'

        return result

#读取ip、密码,ip.csv每一行为192.168.1.1,111111,/var 第一列是ip地址,第二例是密码,第三列是路径iplist=open('ip.csv').readlines()

#存入统计结果

ipresultlist=['IP,FILEPATH,COUNT,TIMECOST\n']

#多进程统计

pool=multiprocessing.Pool(processes=6)

#循环每一行进行统计

for ip in iplist:

    ipin=ip.split(',')

    pool.apply_async(ipresultlist.append(get_total(ipin[0],ipin[1],ipin[2])))

pool.close()

pool.join()

#写入文件

fp=open('tongji_log'+'_'+time.strftime('%Y%m%d%H%M%S',time.localtime())+'.csv','a+')

fp.writelines(ipresultlist)

fp.close()

 

  

你可能感兴趣的:(python)