python 远程统计文件代码分享

yipeiwu_com5年前Python基础

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中map()函数的使用方法示例

python中map()函数的使用方法示例

前言 在python里有一个函数map(),它有点高大上的感觉。本文将详细给大家介绍python中map()函数使用的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介...

Python单元测试框架unittest使用方法讲解

概述 1.测试脚手架(test fixture) 测试准备前要做的工作和测试执行完后要做的工作.包括setUp()和tearDown(). 2.测试案例(test case) 最小的测试...

Pandas实现数据类型转换的一些小技巧汇总

Pandas实现数据类型转换的一些小技巧汇总

前言 Pandas是Python当中重要的数据分析工具,利用Pandas进行数据分析时,确保使用正确的数据类型是非常重要的,否则可能会导致一些不可预知的错误发生。 Pandas 的数据类...

pycharm远程调试openstack代码

pycharm远程调试openstack代码

本文实例为大家分享了pycharm远程调试openstack的具体代码,供大家参考,具体内容如下 1.安装pycharm专业版 本文安装pycharm 2016.2.3专业版。网上教程较...

matplotlib subplots 设置总图的标题方法

如下所示: matplotlib subplots 设置总图的标题 : fig.suptitle(dname,fontsize=16,x=0.53,y=1.05,) 以上这篇matplo...