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

How to use shell and Python to view the network card traffic of Linux server

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use shell and Python to view the network card traffic of the Linux server". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use shell and Python to view the network card traffic of the Linux server".

Sometimes we need to view the network card traffic on the server in real time. Here I write two small scripts, one using shell (first, you can only view one network card at a time) and the other using python (later, you can view more than one network card at a time).

The while true "endless loop" is used in the script, taking a value from "/ proc/net/dev" every 10s and calculating the average bandwidth within 10s based on the difference within 10s; press ctrl+c to stop execution. Scripts are compatible with centos6 and 7

Neither script is too complex, and the comments in the script are more detailed, so I won't explain the content of the script too much.

Directly above the script:

Shell version-use screenshots:

Shell version code:

#! / bin/sh#by ljk 20160526if ["$1" = ""] Then # determines whether the parameter echo-e "\ n use interface_name after the script,like\" script eth0\ "...\ n" exit-1fiecho-e "\ n start monitoring the $1 is followed. Press\ "ctrl+c\" to stop "echo-file=/proc/net/dev # kernel network card information file while true do rx_bytes= `cat $file | grep $1 | sed's / ^ * / / g' | awk-f'[:] +'{print $2}'` # here the sed step is to be compatible with both centos6 and 7 tx_bytes= `cat $file | grep $1 | sed's / ^ * / / g'| awk-f'[:] +'{print $10} '`sleep 10 cat $file | grep $1 | sed's / ^ * / / g' | awk-f'[:] +'{print $2} '`cat `cat $file | grep $1 | sed's / ^ * / / g' | awk-f'[:] +'{print $10}'` # b*8/1024/1024=mb speed_rx= `echo "scale=2 ($rx_bytes_later-$rx_bytes) * 8in 1024 printf 1024 10 "| bc`in `echo" scale=2; ($tx_bytes_later-$tx_bytes) * 8x1024 printf "%-3s%-3.1f%-10s%-4s%-3.1f%-4s\ n" speed_rx mb/s out: $speed_tx mb/sdone

Python version-use screenshots:

Python version code:

#! / bin/env python3 # by ljk 20160526 import os,re,sys,time if len (sys.argv) = 1: print ('\ nmethod of use: please follow the name of the network card, you can follow "single network card" / "multiple network cards separated by spaces".\ n') sys.exit (100) else: print ('start monitoring Press "ctrl+c" to stop\ n') for arg in sys.argv [1:]: # output header header ='- {} bandwidth (mb/s)-'.format (arg) print (header.ljust (35), end='') print () # global values_dic values_dic = {} # define empty dictionary It is used to store the required values def get_values (orders): try: with open ('/ proc/net/dev') as f: lines=f.readlines () # in the following function, so it is more convenient to read for arg in sys.argv [1:]: for line in lines: line=line.lstrip () # to remove the space at the beginning of the line. So that the following split if re.match (arg,line): values = re.split ("[:] +", line) # takes the space and: as the separator values_ [Arg + 'r'+orders] = values [1] # 1 as the receiving value values_ [Arg +' t'+orders] = values [9] # 9 as the sending value # return [values [1] Values [9]] # returns a list of except (fileexistserror,filenotfounderror Permissionerror): print ('open file error') sys.exit (- 1) try: while true: get_values (' first') # the first value is time.sleep (10) get_values ('second') # 10s and the second value is for arg in sys.argv [1:]: r_bandwidth = (int (values_ Dick [Arg +' ringing secondary'])- Int (values_ dicks [Arg + 'ringing cycles first]) / 1024 in 1024 t_bandwidth = (int (values_ dicks [Arg +' tweeds licensed second'])-int (values_ dicks [Arg + 'tympanites first']) / 1024 apprentices 1024 cycles 8 print ('in:'+ str (r_bandwidth)) 2) .ljust (8) + 'out:' + str (round (tweebandwidth2)) .ljust (16), end='') print () values_dic = {} # clear the contents of the dictionary after this cycle except keyboardinterrupt: print ("\ nMuyeh -") Thank you for your reading The above is the content of "how to use shell and Python to view the network card traffic of the Linux server". After the study of this article, I believe you have a deeper understanding of how to use shell and Python to view the network card traffic of the Linux server. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 0

*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

Internet Technology

Wechat

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

12
Report