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()

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

使用Python3 编写简单信用卡管理程序

1、程序执行代码: #Author by Andy #_*_ coding:utf-8 _*_ import os,sys,time Base_dir=os.path.dirname...

Python 中pandas索引切片读取数据缺失数据处理问题

Python 中pandas索引切片读取数据缺失数据处理问题

引入   numpy已经能够帮助我们处理数据,能够结合matplotlib解决我们数据分析的问题,那么pandas学习的目的在什么地方呢? numpy能够帮我们处理处理数值型数据,但是这...

Numpy之reshape()使用详解

Numpy之reshape()使用详解

如下所示: Numpy中reshape的使用方法为:numpy.reshape(a, newshape, order='C') 参数详解: 1.a: type:array_like(伪数...

Python单例模式的两种实现方法

Python单例模式的两种实现方法 方法一  import threading class Singleton(object): __instance = N...

wxPython电子表格功能wx.grid实例教程

本文实例为大家分享了wxPython电子表格功能的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python #encoding: utf8 import wx...