Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Network card traffic monitoring script, realized by python

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Use-h to get help

Execute above Python2.7 version

Script acquisition: https://github.com/raysuen/AdapterMonitor

Content:

#! / usr/bin/env python# _ * _ coding:utf-8_*_# Auth by raysuenimport sys,timeimport reAdapterInfoDict= {"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: # cycle to get file information if line.decode (encoding= "utf8") .find (":")! =-1: # to determine whether it is a network card column if AdapterInfoDict ["InterFace"] = = "all": # to determine whether to obtain the name of the network card All is all network cards if AdapterInfoDict ["ShowSize"] = = "b": # to judge the size displayed. Default is bytes # to generate one-dimensional array records. Network card information: network card name, incoming traffic. Outbound 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])] elif AdapterInfoDict ["ShowSize"] = "m": face= [line.decode (encoding= "utf8"). Split (":") [0] .strip (), round (int (line.decode (encoding= "utf8"). Split () [1]) / 1024Unip 1024) Round (int (line.decode (encoding= "utf8"). Split () [9])] else: print ("The value of-s is invalid,you can use-h to get help.") Exit (69) faces.append (face) # stores the one-dimensional array of each network card information into a two-dimensional array else: for facename in AdapterInfoDict ["InterFace"] .split (","): # determines the name of the network card, which can be multiple network cards. Separate multiple network cards 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])] elif AdapterInfoDict ["ShowSize"] = "m": face = [line.decode (encoding= "utf8") .split (":") [0] .strip () Round (int (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 facesdef 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 an 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-an 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-an in-s k "") if _ _ Name__ = "_ _ main__": num=1 # counter Record the number of exits of the current parameter subscript exitnum=0 # when exiting # get the parameter if len (sys.argv) > 1: # to determine whether there is a parameter input 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 >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 264

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report