python 遠程統(tǒng)計文件代碼分享
更新時間:2015年05月14日 12:01:23 投稿:hebedich
享一個Python獲取遠程文件大小的函數代碼,簡單實用,是學習Python編程的基礎實例。
python 遠程統(tǒng)計文件
#!/usr/bin/python
#encoding=utf-8
import time
import os
import paramiko
import multiprocessing
#統(tǒng)計文件數量
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()
#存入統(tǒng)計結果
ipresultlist=['IP,FILEPATH,COUNT,TIMECOST\n']
#多進程統(tǒng)計
pool=multiprocessing.Pool(processes=6)
#循環(huán)每一行進行統(tǒng)計
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讀取系統(tǒng)文件夾內所有文件并統(tǒng)計數量的方法
- Python統(tǒng)計純文本文件中英文單詞出現(xiàn)個數的方法總結【測試可用】
- Python實現(xiàn)統(tǒng)計文本文件字數的方法
- Python統(tǒng)計文件中去重后uuid個數的方法
- Python實現(xiàn)對excel文件列表值進行統(tǒng)計的方法
- python統(tǒng)計文本文件內單詞數量的方法
- Python3讀取UTF-8文件及統(tǒng)計文件行數的方法
- python腳本實現(xiàn)統(tǒng)計日志文件中的ip訪問次數代碼分享
- 使用python統(tǒng)計文件行數示例分享
- python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計
相關文章
Python?xlwt工具使用詳解,生成excel欄位寬度可自適應內容長度
這篇文章主要介紹了Python?xlwt工具使用詳解,生成excel欄位寬度可自適應內容長度,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

