1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#!/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()
|
以上所述就是本文的全部内容了,希望大家能够喜欢。