pythonリモート統計ファイル
5990 ワード
#!/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('
')
count=int(stdout.read().strip('
'))
endtime=time.time()
caltime=endtime-starttime
result=ip+','+filepath.strip('
')+','+str(count)+','+str(caltime)+'
'
return result
except:
result=ip+','+filepath.strip('
')+','+'failed'+'
'
return result
# ip、 ,ip.csv 192.168.1.1,111111,/var ip , , iplist=open('ip.csv').readlines()
#
ipresultlist=['IP,FILEPATH,COUNT,TIMECOST
']
#
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()