使用-h獲取幫助
成都創(chuàng)新互聯(lián)公司成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元雨湖做網(wǎng)站,已為上家服務(wù),為雨湖各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
在Python2.7版本以上執(zhí)行
腳本獲?。? https://github.com/raysuen/AdapterMonitor
內(nèi)容:
#!/usr/bin/env python # _*_coding:utf-8_*_ # Auth by raysuen import sys,time import re AdapterInfoDict={ "InterFace":"all", "Interval":1, "NumberOfDis":None, "Action":"all", "ShowSize":"b" } def GetAdapterInfo(): faces=[] # f=open(r"C:\Users\Administrator\Downloads\dev","rb") f = open(r"/proc/net/dev", "rb") for line in f: #循環(huán)獲取文件信息 if line.decode(encoding="utf8").find(":") != -1: #判斷是否為網(wǎng)卡列 if AdapterInfoDict["InterFace"] == "all": #判斷獲取網(wǎng)卡的名稱,all為全部網(wǎng)卡 if AdapterInfoDict["ShowSize"] == "b": #判斷顯示的大小,默認(rèn)為bytes #生成一維數(shù)組記錄,網(wǎng)卡信息:網(wǎng)卡名稱,進(jìn)流量,出流量 face=[line.decode(encoding="utf8").split(":")[0].strip(),int(line.decode(encoding="utf8").split()[1]),int(line.decode(encoding="utf8").split()[9])] elif AdapterInfoDict["ShowSize"] == "k": face=[line.decode(encoding="utf8").split(":")[0].strip(),round(int(line.decode(encoding="utf8").split()[1])/1024),round(int(line.decode(encoding="utf8").split()[9])/1024)] elif AdapterInfoDict["ShowSize"] == "m": face=[line.decode(encoding="utf8").split(":")[0].strip(),round(int(line.decode(encoding="utf8").split()[1])/1024/1024),round(int(line.decode(encoding="utf8").split()[9])/1024/1024)] else: print("The value of -s is invalid,you can use -h to get help.") exit(69) faces.append(face) #把每個網(wǎng)卡信息的一維數(shù)組存入二維數(shù)組 else: for facename in AdapterInfoDict["InterFace"].split(","): #判斷網(wǎng)卡名稱,可以為多個網(wǎng)卡,多個網(wǎng)卡用逗號分隔 if line.decode(encoding="utf8").split(":")[0].strip() == facename: if AdapterInfoDict["ShowSize"] == "b": face = [line.decode(encoding="utf8").split(":")[0].strip(), int(line.decode(encoding="utf8").split()[1]), int(line.decode(encoding="utf8").split()[9])] elif AdapterInfoDict["ShowSize"] == "k": face = [line.decode(encoding="utf8").split(":")[0].strip(), round(int(line.decode(encoding="utf8").split()[1]) / 1024), round(int(line.decode(encoding="utf8").split()[9]) / 1024)] elif AdapterInfoDict["ShowSize"] == "m": face = [line.decode(encoding="utf8").split(":")[0].strip(), round(int(line.decode(encoding="utf8").split()[1]) / 1024 / 1024), round(int(line.decode(encoding="utf8").split()[9]) / 1024 / 1024)] else: print("The value of -s is invalid,you can use -h to get help.") exit(69) faces.append(face) return faces def help_func(): print(""" NAME: AdapterMonitor --Display net interface netflow SYNOPSIS: AdapterMoniter [-f] [interface names] [-i] [interval time] [-n] [display number] [-a] [action] [-s] [show size] DESCRIPTION: -f specify interface names.values is interface names or all,default all. You can specify a name,also some names. If the names is more one,you can use comma as separator. Example: AdapterMoniter.py -f eth0 AdapterMoniter.py -f eth0,eth2 -i specify a interval time to display,defaul 1 second. Unit: second -n to display how many times you want.Default: None,means unlimited number. Example: AdapterMoniter.py -n 2 -a to display what action you want,IN/OUT/ALL.Defaul: all. Example: AdapterMoniter.py -a in -s to display the netflow size.Default: b(bytes) values: b(bytes)/k(KB)/m(MB) Example: AdapterMoniter.py -s k EXAMPLE: AdapterMoniter.py -f eth0 -i 2 -n 10 -a in -s k """) if __name__ == "__main__": num=1 #計數(shù)器,記錄當(dāng)前參數(shù)下標(biāo) exitnum=0 #退出時的退出數(shù) #獲取參數(shù) if len(sys.argv) > 1: #判斷是否有參數(shù)輸入 while num < len(sys.argv): if sys.argv[num] == "-h": help_func() #執(zhí)行幫助函數(shù) exitnum = 0 exit(exitnum) elif sys.argv[num] == "-f": num += 1 #下標(biāo)向右移動一位 if num >= len(sys.argv): #判斷是否存在當(dāng)前下標(biāo)的參數(shù) exitnum = 99 print("The parameter must be specified a value,-f.") exit(exitnum) elif re.match("^-",sys.argv[num]) == None: #判斷當(dāng)前參數(shù)是否為-開頭,None為非-開頭 AdapterInfoDict["InterFace"]=sys.argv[num] num += 1 else: print("Please specify a valid value for -f.") exitnum = 98 exit(exitnum) elif sys.argv[num] == "-i": num += 1 if num >= len(sys.argv): exitnum = 97 print("The parameter must be specified a value,-i.") exit(exitnum) elif re.match("^-",sys.argv[num]) == None: if sys.argv[num].isdigit() == True: #判斷是否為正整數(shù) AdapterInfoDict["Interval"]=sys.argv[num] num += 1 else: print("The value of -i must be digit.") exitnum = 96 exit(exitnum) else: print("Please specify a valid value for -i.") exitnum = 95 exit(exitnum) elif sys.argv[num] == "-n": num += 1 if num >= len(sys.argv): exitnum = 94 print("The parameter must be specified a value,-n.") exit(exitnum) elif re.match("^-",sys.argv[num]) == None: if sys.argv[num].isdigit() == True: AdapterInfoDict["NumberOfDis"]=sys.argv[num] num += 1 else: print("The value of -n must be digit.") exitnum = 93 exit(exitnum) else: print("Please specify a valid value for -n.") exitnum = 92 exit(exitnum) elif sys.argv[num] == "-a": num += 1 if num >= len(sys.argv): exitnum = 91 print("The parameter must be specified a value,-a.") exit(exitnum) elif re.match("^-",sys.argv[num]) == None: AdapterInfoDict["Action"]=sys.argv[num] num += 1 else: print("Please specify a valid value for -a.") exitnum = 90 exit(exitnum) elif sys.argv[num] == "-s": num += 1 if num >= len(sys.argv): exitnum = 89 print("The parameter must be specified a value,-s.") exit(exitnum) elif re.match("^-",sys.argv[num]) == None: AdapterInfoDict["ShowSize"]=sys.argv[num] num += 1 else: print("Please specify a valid value for -s.") exitnum = 90 exit(exitnum) #獲取開始的網(wǎng)卡信息 facesPre = GetAdapterInfo() if AdapterInfoDict["NumberOfDis"] == None: #判斷顯示次數(shù),None為無限次 t = 0 #計數(shù)器,沒10次打印一下行頭 while True: time.sleep(int(AdapterInfoDict["Interval"])) #睡眠,根據(jù)時間間隔 facesSuf = GetAdapterInfo() #獲取比對的結(jié)束網(wǎng)卡信息 if AdapterInfoDict["Action"] == "all": #判斷動作,是顯示進(jìn),出或是全部的流量 if t % 10 == 0: print("%s:%s%s" % ("FaceName".rjust(10), "In".rjust(30), "Out".rjust(30))) print("%s"%"-".center(70,"-")) elif AdapterInfoDict["Action"] == "in": if t % 10 == 0: print("%s:%s" % ("FaceName".rjust(10), "In".rjust(30))) print("%s" % "-".center(40,"-")) elif AdapterInfoDict["Action"] == "out": if t % 10 == 0: print("%s:%s" % ("FaceName".rjust(10), "Out".rjust(30))) print("%s" % "-".center(40,"-")) t += 1 for i in range(len(facesPre)): if AdapterInfoDict["Action"] == "all": print("%s:%s%s"%(facesPre[i][0].rjust(10),str(facesSuf[i][1]-facesPre[i][1]).rjust(30),str(facesSuf[i][2]-facesPre[i][2]).rjust(30))) elif AdapterInfoDict["Action"] == "in": print("%s:%s" % (facesPre[i][0].rjust(10), str(facesSuf[i][1] - facesPre[i][1]).rjust(30))) elif AdapterInfoDict["Action"] == "out": print("%s:%s" % (facesPre[i][0].rjust(10), str(facesSuf[i][2] - facesPre[i][2]).rjust(30))) else: print("The value of -a is a invalid action which you entered.") print("You can use -h to get help.") exitnum=89 exit(exitnum) facesPre=facesSuf # time.sleep(int(AdapterInfoDict["Interval"])) else: for t in range(int(AdapterInfoDict["NumberOfDis"])): #安裝顯示次數(shù)循環(huán) time.sleep(int(AdapterInfoDict["Interval"])) facesSuf = GetAdapterInfo() #輸出打印行頭 if AdapterInfoDict["Action"] == "all": if t % 10 == 0: print("%s:%s%s" % ("FaceName".rjust(10), "In".rjust(30), "Out".rjust(30))) print("%s" % "-".center(70,"-")) elif AdapterInfoDict["Action"] == "in": if t % 10 == 0: print("%s:%s" % ("FaceName".rjust(10), "In".rjust(30))) print("%s" % "-".center(40,"-")) elif AdapterInfoDict["Action"] == "out": if t % 10 == 0: print("%s:%s" % ("FaceName".rjust(10), "Out".rjust(30))) print("%s" % "-".center(40,"-")) for i in range(len(facesPre)): if AdapterInfoDict["Action"] == "all": print("%s:%s%s" % (facesPre[i][0].rjust(10), str(facesSuf[i][1] - facesPre[i][1]).rjust(30), str(facesSuf[i][2] - facesPre[i][2]).rjust(30))) elif AdapterInfoDict["Action"] == "in": print("%s:%s" % (facesPre[i][0].rjust(10), str(facesSuf[i][1] - facesPre[i][1]).rjust(30))) elif AdapterInfoDict["Action"] == "out": print("%s:%s" % (facesPre[i][0].rjust(10), str(facesSuf[i][2] - facesPre[i][2]).rjust(30))) else: print("The value of -a is a invalid action which you entered.") print("You can use -h to get help.") exitnum = 88 exit(exitnum) facesPre = facesSuf # time.sleep(int(AdapterInfoDict["Interval"])) exit(exitnum)
網(wǎng)站欄目:網(wǎng)卡流量監(jiān)控腳本,python實(shí)現(xiàn)
文章分享:http://jinyejixie.com/article22/ipjcjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)頁設(shè)計公司、建站公司、動態(tài)網(wǎng)站、Google、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)