EveryDay-Shell之"ifconfig输出详解“——netspeed.sh网速监控脚本

对应文件中,比如p1p1设备对应目录:
$cd /sys/class/net/p1p1/statistics

$ls -l

 -r--r--r-- 1 root root 4096  2月  4 14:35 collisions
-r--r--r-- 1 root root 4096  2月  4 14:35 multicast
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_bytes
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_compressed
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_crc_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_dropped
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_fifo_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_frame_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_length_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_missed_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_over_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 rx_packets
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_aborted_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_bytes
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_carrier_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_compressed
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_dropped
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_fifo_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_heartbeat_errors
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_packets
-r--r--r-- 1 root root 4096  2月  4 14:35 tx_window_errors

$cat rx_bytes(接收总数据大小B为单位,即下行)
226698240

$cat tx_bytes(发送总数据打包B为单位,即上行)
10534137

        
          根据这些文件存储的数据,可以设计一个监控网速的shell脚本:
#!/usr/bin/env bash

# Program: netspeed.sh
# Author: LeslieChu
# Date: 2012-02-04
# Version: 1.0

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ -z "$1" ]; 
then
        echo
        echo "Usage:  $0   eth0";
        exit  1
fi

while true;
do
        R_1=`cat /sys/class/net/$1/statistics/rx_bytes`;
        T_1=`cat /sys/class/net/$1/statistics/tx_bytes`;

        sleep 1;

        R_2=`cat /sys/class/net/$1/statistics/rx_bytes`;
        T_2=`cat /sys/class/net/$1/statistics/tx_bytes`;

        R_Byte_PerSec=$(expr   $R_2  -   $R_1);
        T_Byte_PerSec=$(expr    $T_2  -   $T_1);

         R_KB_PerSec=$(expr     ${R_Byte_PerSec}   /    1024);     #记得“/”左右需要空格,否则无效
         T_KB_PerSec=$(expr      ${T_Byte_PerSec}   /  1024);

         R_MB_PerSec=$(expr     ${R_KB_PerSec}    /    1024);
         T_MB_PerSec=$(expr      ${T_KB_PerSec}    /     1024);

        echo -en "$1->\tTX:\t${T_KB_PerSec} KB/s, \t${T_MB_PerSec}MB/s; "
        echo -e     " \tRX:\t${R_KB_PerSec}KB/s, \t${R_MB_PerSec}MB/s."

done

        执行结果:
$./netspeed.sh
Usage:  ./netspeed.sh   eth0


  $./netspeed.sh p1p1

p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,    0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,    0MB/s.
p1p1->      TX:     3 KB/s,    0MB/s;     RX:    0KB/s,    0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,    0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,    0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,    0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,    0MB/s.
p1p1->      TX:     8 KB/s,    0MB/s;     RX:  20KB/s,    0MB/s.
p1p1->      TX:   10 KB/s,    0MB/s;     RX:  21KB/s,    0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,     0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,     0MB/s.
p1p1->      TX:     5 KB/s,    0MB/s;     RX:    1KB/s,     0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,     0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    0KB/s,     0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    3KB/s,     0MB/s.
p1p1->      TX:     0 KB/s,    0MB/s;     RX:    1KB/s,     0MB/s.
^C
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值