教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 统计网卡流量的二个shell脚本(ifconfig)(图文)

统计网卡流量的二个shell脚本(ifconfig)(图文)

发布时间:2014-11-12   编辑:jiaochengji.com
本文介绍下,用ifconfig命令不间断输出与统计网卡流量的几个shell脚本,有代码有示例,超级实用的小脚本,不容错过。

一,使用ifconfig命令监测网卡eth0的实时流量。

脚本内容:
 

复制代码 代码示例:

#!/bin/bash
#filename interface.sh
# 统计网卡流量

n=10 #监测10次

date
rm -rf /home/toplover/ifconfig_log

while (( $n >= 0  ))
do
  n=$(($n - 1));
  date >> /home/toplover/ifconfig_log
  ifconfig eth0 >> /home/toplover/ifconfig_log
  sleep 1
done

grep "RX bytes:" /home/toplover/ifconfig_log | awk -F"[:| ]" '{print $13}' | awk 'BEGIN{tmp=$1}{if(FNR > 1)print $1-tmp}{tmp=$1}'

代码说明:
1,使用ifconfig获取当前网卡的实时流量数据,共监测10次。
间隔时间一秒。
并将实时网卡数据写入文件/home/toplover/ifconfig_log中。
2,通过awk命令的数据处理,打印出直观的数据。

调用示例:
 

统计网卡流量

输出结果:
 

统计网卡流量

以下是对以上脚本的改进版,提供网卡接口为参数,也是此脚本的唯一参数。
 

复制代码 代码示例:

#!/bin/bash
#filename interface.sh
# 统计网卡流量

if [ -n "$1" ]; then
  eth_name=$1
else
  eth_name="eth0"
fi

n=10 #监测10次

date
rm -rf /home/toplover/ifconfig_log

while (( $n >= 0  ))
do
  n=$(($n - 1));
  date >> /home/toplover/ifconfig_log
  ifconfig $eth_name >> /home/toplover/ifconfig_log
  sleep 1
done

grep "RX bytes:" /home/toplover/ifconfig_log | awk -F"[:| ]" '{print $13}' | awk 'BEGIN{tmp=$1}{if(FNR > 1)print $1-tmp}{tmp=$1}'

调用示例,比如要测试eth1网卡接口上的流量,则这样:
 

复制代码 代码示例:
#sh interface.sh eth1

二,使用ifconfig及awk、expr等命令,计算网口上注入、流出网卡的流量,及流量汇总数据。

脚本内容:
 

复制代码 代码示例:

#!/bin/bash
#filename interface2.sh

if [ -n "$1" ]; then
  eth_name=$1
else
  eth_name="eth0"
fi
i=0
send_o=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
recv_o=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
send_n=$send_o
recv_n=$recv_o
while [ $i -le 100000 ]; do
  send_l=$send_n
  recv_l=$recv_n
  sleep 1
  send_n=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
  recv_n=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
  i=`expr $i + 1`
  send_r=`expr $send_n - $send_l`
  recv_r=`expr $recv_n - $recv_l`
  total_r=`expr $send_r + $recv_r`
  send_ra=`expr \( $send_n - $send_o \) / $i`
  recv_ra=`expr \( $recv_n - $recv_o \) / $i`
  total_ra=`expr $send_ra + $recv_ra`
  sendn=`ifconfig $eth_name | grep bytes | awk -F \( '{print $3}' | awk -F \) '{print $1}'`
  recvn=`ifconfig $eth_name | grep bytes | awk -F \( '{print $2}' | awk -F \) '{print $1}'`
  clear
  echo "-------------------------------------------------------------------------"
  echo  "Last second  :   Send rate: $send_r Bytes/sec  Recv rate: $recv_r Bytes/sec  Total rate: $total_r Bytes/sec"
  echo  "Average value:   Send rate: $send_ra Bytes/sec  Recv rate: $recv_ra Bytes/sec  Total rate: $total_ra Bytes/sec"
  echo  "Total traffic after startup:    Send traffic: $sendn  Recv traffic: $recvn"
done

调用示例:
 

统计网卡流量

输出结果:
 

统计网卡流量

说明:注意观察红线标注的数据,均是实时变化的。
为了直观的看出流量的实时变化,我们这里循环调用1万次监测数据。

您可能感兴趣的文章:
统计网卡流量的二个shell脚本(ifconfig)(图文)
ifconfig统计网卡流量的shell脚本
shell脚本计算Linux网卡流量
检测网卡流量的shell脚本
监控网卡流量的shell脚本分享
计算网卡流量的shell脚本(代码分享)
实时查看Linux网卡流量的shell脚本分享(图文)
linux系统监控脚本
实时监测vps主机流量的shell脚本
一个自动检测eth0网卡带宽脚本

关键词: ifconfig  网卡流量   
[关闭]
~ ~