Linux服务器网卡流量查看方法 shell和Python各一枚

作者:kaifly 时间:2023-10-07 09:31:19 

有时我们需要较为实时的查看服务器上的网卡流量,这里我写了两个小脚本,一个用shell(先写的,一次只能查看一个网卡),另一个用Python(后写的,一次可查看多个网卡)。

脚本中都用了while true“死循环”,每隔10s从“/proc/net/dev”中取一次值并根据10s内的差值计算10s内的平均带宽;按ctrl+c停止执行。脚本兼容centos6和7

两个脚本都不太复杂,而且脚本中注释也比较细致,所以我就不过多解释脚本内容了。

直接上图上脚本:

shell版–使用截图:

Linux服务器网卡流量查看方法 shell和Python各一枚 

shell版代码:


#!/bin/sh
#by ljk 20160526

if [ "$1" = "" ];then  #判断后面是否有跟参数
 echo -e "\n   use interface_name after the script,like \"script eth0\"...\n"
 exit -1
fi

echo -e "\n   start monitoring the $1,press \"ctrl+c\" to stop"
echo ----------------------------------------------------------

file=/proc/net/dev  #内核网卡信息文件
while true
 do
 RX_bytes=`cat $file|grep $1|sed 's/^ *//g'|awk -F'[ :]+' '{print $2}'`  #这里sed这一步为了同时兼容centos6和7
 TX_bytes=`cat $file|grep $1|sed 's/^ *//g'|awk -F'[ :]+' '{print $10}'`
 sleep 10
 RX_bytes_later=`cat $file|grep $1|sed 's/^ *//g'|awk -F'[ :]+' '{print $2}'`
 TX_bytes_later=`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)*8/1024/1024/10"|bc`
 speed_TX=`echo "scale=2;($TX_bytes_later - $TX_bytes)*8/1024/1024/10"|bc`

printf "%-3s %-3.1f %-10s %-4s %-3.1f %-4s\n" IN: $speed_RX Mb/s OUT: $speed_TX Mb/s
done

Python版–使用截图:

Linux服务器网卡流量查看方法 shell和Python各一枚

Linux服务器网卡流量查看方法 shell和Python各一枚

Linux服务器网卡流量查看方法 shell和Python各一枚

Python版代码:


#!/bin/env python3
#by ljk 20160526

import os,re,sys,time

if len(sys.argv) == 1:
 print('\n使用方法:请跟上网卡名称,可接"单个网卡"/"多个网卡,以空格分开".\n')
 sys.exit(100)
else:
 print('start monitoring,press "ctrl+c" to stop\n')

for arg in sys.argv[1:]:  #输出标头
   header = '------{} bandwidth(Mb/s)------'.format(arg)
   print(header.ljust(35),end='')
 print()

#global values_dic
 values_dic = {}  #定义空字典,用来在下面函数中存放各网卡的各项需要用到的值

def get_values(orders):
   try:
     with open('/proc/net/dev') as f:
       lines=f.readlines()  #内容不多,一次性读取较方便
       for arg in sys.argv[1:]:
         for line in lines:
           line=line.lstrip()  #去掉行首的空格,以便下面split
           if re.match(arg,line):
             values = re.split("[ :]+",line)  #以空格和:作为分隔符
             values_dic[arg+'r'+orders]=values[1]  #1为接收值
             values_dic[arg+'t'+orders]=values[9]  #9为发送值
             #return [values[1],values[9]]  #可返回列表
   except (FileExistsError,FileNotFoundError,PermissionError):
     print('open file error')
     sys.exit(-1)

try:
   while True:
     get_values('first')  #第一次取值
     time.sleep(10)
     get_values('second')  #10s后第二次取值

for arg in sys.argv[1:]:
       r_bandwidth = (int(values_dic[arg+'r'+'second']) - int(values_dic[arg+'r'+'first']))/1024/1024/10*8
       t_bandwidth = (int(values_dic[arg+'t'+'second']) - int(values_dic[arg+'t'+'first']))/1024/1024/10*8
       print('IN: '+str(round(r_bandwidth,2)).ljust(8)+' OUT: '+str(round(t_bandwidth,2)).ljust(16),end='')

print()
     values_dic = {}  #清空本次循环后字典的内容
 except KeyboardInterrupt:
   print("\n-----bye-----")

这俩脚本使用起来都还是很方便实用的,共享出来希望能给朋友们工作中带来一点方便。

来源:http://blog.csdn.net/kai404/article/details/52853656

标签:Linux,网卡流量,shell,python
0
投稿

猜你喜欢

  • javascript 缓冲效果实现代码 推荐

    2024-04-29 13:36:08
  • Mysql 查询JSON结果的相关函数汇总

    2024-01-23 22:30:16
  • 利用PHP实现词法分析器与自定义语言

    2024-05-02 17:33:35
  • python生成随机图形验证码详解

    2023-11-20 22:28:10
  • JS与Ajax Get和Post在使用上的区别实例详解

    2024-04-23 09:07:40
  • Python while true实现爬虫定时任务

    2021-02-10 13:35:30
  • ASP中Cookies集合使用方法详解

    2007-09-14 10:16:00
  • 用Python爬取某乎手机APP数据

    2021-02-03 17:04:59
  • python开发游戏的前期准备

    2022-01-06 12:58:01
  • Pyinstaller加密打包成反编译可执行文件

    2022-06-20 14:23:06
  • Vue Router中应用中间件的方法

    2024-05-09 10:42:43
  • python关闭占用端口方式

    2022-03-26 14:10:53
  • 基于python实现模拟数据结构模型

    2022-11-12 23:44:01
  • 总结近几年Pytorch基于Imgagenet数据集图像分类模型

    2023-01-06 01:15:31
  • 在OracleE数据库的字段上建立索引的方法

    2009-02-26 10:34:00
  • python实现折半查找和归并排序算法

    2023-06-24 00:08:01
  • Python进阶篇之多线程爬取网页

    2023-01-15 13:22:39
  • pytorch中DataLoader()过程中遇到的一些问题

    2022-01-17 18:36:11
  • MySQL下高可用故障转移方案MHA的超级部署教程

    2024-01-27 13:53:23
  • Python编程实现二分法和牛顿迭代法求平方根代码

    2022-01-03 12:24:46
  • asp之家 网络编程 m.aspxhome.com