網路卡流量監控指令碼,python實現
使用-h獲取幫助
在Python2.7版本以上執行
指令碼獲取:
內容:
#!/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: #迴圈獲取檔案資訊 if line.decode(encoding="utf8").find(":") != -1: #判斷是否為網路卡列 if AdapterInfoDict["InterFace"] == "all": #判斷獲取網路卡的名稱,all為全部網路卡 if AdapterInfoDict["ShowSize"] == "b": #判斷顯示的大小,預設為bytes #生成一維陣列記錄,網路卡資訊:網路卡名稱,進流量,出流量 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) #把每個網路卡資訊的一維陣列存入二維陣列 else: for facename in AdapterInfoDict["InterFace"].split(","): #判斷網路卡名稱,可以為多個網路卡,多個網路卡用逗號分隔 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,eth1 -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 #計數器,記錄當前引數下標 exitnum=0 #退出時的退出數 #獲取引數 if len(sys.argv) > 1: #判斷是否有引數輸入 while num < len(sys.argv): if sys.argv[num] == "-h": help_func() #執行幫助函式 exitnum = 0 exit(exitnum) elif sys.argv[num] == "-f": num += 1 #下標向右移動一位 if num >= len(sys.argv): #判斷是否存在當前下標的引數 exitnum = 99 print("The parameter must be specified a value,-f.") exit(exitnum) elif re.match("^-",sys.argv[num]) == None: #判斷當前引數是否為-開頭,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: #判斷是否為正整數 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) #獲取開始的網路卡資訊 facesPre = GetAdapterInfo() if AdapterInfoDict["NumberOfDis"] == None: #判斷顯示次數,None為無限次 t = 0 #計數器,沒10次列印一下行頭 while True: 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,"-")) 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"])): #安裝顯示次數迴圈 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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28572479/viewspace-2649577/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Linux下針對伺服器網路卡流量和磁碟的監控指令碼Linux伺服器指令碼
- 例項程式碼分享Python實現Linux監控PythonLinux
- 【shell】磁碟監控指令碼指令碼
- 在Linux中,如何實時監控網路流量?Linux
- iftop--實時網路介面流量監控工具
- PostgreSQL之鎖監控指令碼SQL指令碼
- 使用Shell指令碼程式監控網站URL是否正常指令碼網站
- TiDB監控實現--存活監控TiDB
- 監控系統告警指令碼集合指令碼
- iOS 流量監控分析iOS
- MySQL 5.6大查詢和大事務監控指令碼(Python 2)MySql指令碼Python
- 實時監控網路流量,精準辨別網路效能瓶頸
- 啟停無線網路卡bat指令碼BAT指令碼
- centos6 修改網路卡名指令碼CentOS指令碼
- Python實現遠端埠監控例項Python
- 教你用vbs指令碼獲取網路卡MAC,CPUID,硬碟序列號的實現程式碼指令碼MacUI硬碟
- Shell 系統資訊監控指令碼指令碼
- Asp.Net 5分鐘實現網頁實時監控程式碼ASP.NET網頁
- Linux 流量監控工具 iftopLinux
- 分享實用監控指令碼:使用Shell檢查程式是否存在指令碼
- 案例五:shell指令碼實現定時監控http服務的執行狀態指令碼HTTP
- 監控磁碟使用率的shell指令碼指令碼
- centos 監控web站點是否500 指令碼CentOSWeb指令碼
- shell指令碼:監控MySQL服務是否正常指令碼MySql
- Shell指令碼監控MySQL主從狀態指令碼MySql
- sqlserver監控指令碼_發現某個等待就發出郵件SQLServer指令碼
- 監控雲流量的七種QoS最佳實踐
- python實現自動搶課指令碼Python指令碼
- Python tkinter 實現 指令碼工具 GUI模版Python指令碼GUI
- 蘋果iPhone XR/XS Max怎麼監控流量?iPhone流量監控設定教程蘋果iPhone
- 如何實現一個IOS網路監控元件iOS元件
- 多網路卡環境下利用策略路由實現網路流量同進同出[轉載]路由
- shell指令碼監控啟動停止weblogic服務指令碼Web
- 關於前端指令碼異常監控的思考前端指令碼
- 教你如何基於python實現指令碼加密Python指令碼加密
- 如何實現監控手機螢幕?(附原始碼)原始碼
- 網路流量監控軟體:NetWorker Pro for mac 中文版Mac
- 北京智和信通網路流量監控分析平臺