python 远程统计文件代码分享

WBOY
Release: 2016-06-10 15:12:36
Original
1089 people have browsed it

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()
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template