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程序(.py)转换为Windows可执行文件(.exe)

详解将Python程序(.py)转换为Windows可执行文件(.exe)

python开发者向普通windows用户分享程序,要给程序加图形化的界面(传送门:这可能是最好玩的python GUI入门实例! /post/165763.htm),并要将软件打包为可...

python使用PIL模块实现给图片打水印的方法

本文实例讲述了python使用PIL模块实现给图片打水印的方法。分享给大家供大家参考。具体实现方法如下: import Image, ImageEnhance def reduce_...

Python中使用双下划线防止类属性被覆盖问题

Python中使用双下划线防止类属性被覆盖问题

在使用Python编写面向对象的代码时,我们会常常使用“继承”这种开发方式。例如下面这一段代码: class Info: def __init__(self): pass...

初学python数组的处理代码

初学python,小缘缘出了几道题: 有一 list a = [1, 2, 3, 4, 5, 6] 请将 a 依 0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出,...

python远程邮件控制电脑升级版

由于前边Python3.4实现远程控制电脑开关机写的远程操控电脑,使用的POP登陆有使用频率限制,导致非常被动,有时候邮件无法读取,下面改用POST网易邮箱的方法,获取邮件 impo...