python 远程统计文件代码分享

yipeiwu_com6年前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多进程实现文件下载传输功能

本文实例为大家分享了python多进程实现文件下载传输功能的具体代码,供大家参考,具体内容如下 需求: 实现文件夹拷贝功能(包括文件内的文件),并打印拷贝进度 模块: os模块 mu...

python3+selenium实现126邮箱登陆并发送邮件功能

本文实例为大家分享了python3实现126邮箱登陆并发送邮件的具体代码,供大家参考,具体内容如下 基于selenium,使用chrome浏览器,完成126邮箱登陆并发送发邮件功能,暂时...

Python 判断 有向图 是否有环的实例讲解

实例如下: import numpy from numpy import * def dfs( v ): vis[v] = -1 flag = 0 for i in range...

python使用原始套接字发送二层包(链路层帧)的方法

发送端代码: #!/usr/bin/python # -*- coding: UTF-8 -*- import socket import struct raw_socket =...

numpy ndarray 取出满足特定条件的某些行实例

在进行物体检测的ground truth boxes annotations包围框坐标数据整理时,需要实现这样的功能: numpy里面,对于N*4的数组,要实现对于每一行,如果第3列和第...