Linux查看网卡流量的shell脚本

公司内网不通外网,不能下载网络监控工具,就可以使用下面的脚本,复制过去监测一下。

#!/bin/bash
R2=`cat /sys/class/net/$1/statistics/rx_bytes`
T2=`cat /sys/class/net/$1/statistics/tx_bytes`
NUM=100000
if [ -z "$1" ]; then
        echo
        echo usage: $0 network-interface
        echo
        echo e.g. $0 eth0
        echo
        exit
fi
while true
do
        R1=`cat /sys/class/net/$1/statistics/rx_bytes`
        T1=`cat /sys/class/net/$1/statistics/tx_bytes`
        TBPS=`expr $T1 - $T2`
        RBPS=`expr $R1 - $R2`
        TKBPS=`expr $TBPS / 1024`
        RKBPS=`expr $RBPS / 1024`
        eval `date "+day=%d; month=%m; year=%Y; hour=%H; minute=%M second=%S"`
        INSTFIL4="$hour:$minute:$second"
	if [ $RKBPS -gt 1024 ] || [ $TKBPS -gt 1024 ];then
		RMBPS=$(printf "%.2f" `echo "scale=2;$RKBPS/1024"|bc`)
		TMBPS=$(printf "%.2f" `echo "scale=2;$TKBPS/1024"|bc`)
		echo -e "$INSTFIL4 发送 $1: \033[31m$TMBPS\033[0m mb/s 下载 \033[32m$RMBPS\033[0m mb/s "
        elif [ $RKBPS -gt 1024 ] && [ $TKBPS -lt 1024 ];then
                RMBPS=$(printf "%.2f" `echo "scale=2;$RKBPS/1024"|bc`)
                echo -e "$INSTFIL4 发送 $1: \033[31m$TKBPS\033[0m kb/s 下载 \033[32m$RMBPS\033[0m mb/s "
        elif [ $RKBPS -lt 1024 ] && [ $TKBPS -gt 1024 ];then
                TMBPS=$(printf "%.2f" `echo "scale=2;$TKBPS/1024"|bc`)
                echo -e "$INSTFIL4 发送 $1: \033[31m$TMBPS\033[0m mb/s 下载 \033[32m$RKBPS\033[0m kb/s "
        else
		echo -e "$INSTFIL4 发送 $1: \033[31m$TKBPS\033[0m kb/s 下载 \033[32m$RKBPS\033[0m kb/s "
	fi
        R2=`cat /sys/class/net/$1/statistics/rx_bytes`
        T2=`cat /sys/class/net/$1/statistics/tx_bytes`
        sleep 1s
done

vyos网络操作系统安装不了bc命令,脚本不能运行,所以改成下面的

#!/bin/bash
R2=`cat /sys/class/net/$1/statistics/rx_bytes`
T2=`cat /sys/class/net/$1/statistics/tx_bytes`
NUM=100000
if [ -z "$1" ]; then
        echo
        echo usage: $0 network-interface
        echo
        echo e.g. $0 eth0
        echo
        exit
fi
while true
do
        R1=`cat /sys/class/net/$1/statistics/rx_bytes`
        T1=`cat /sys/class/net/$1/statistics/tx_bytes`
        TBPS=`expr $T1 - $T2`
        RBPS=`expr $R1 - $R2`
        TKBPS=`expr $TBPS / 1024`
        RKBPS=`expr $RBPS / 1024`
        eval `date "+day=%d; month=%m; year=%Y; hour=%H; minute=%M second=%S"`
        INSTFIL4="$hour:$minute:$second"
	if [ $RKBPS -gt 1024 ] || [ $TKBPS -gt 1024 ];then
#		RMBPS=$(printf "%.2f" `echo "scale=2;$RKBPS/1024"|bc`)
#		TMBPS=$(printf "%.2f" `echo "scale=2;$TKBPS/1024"|bc`)
		RMBPS=`expr $RKBPS / 1024`
		TMBPS=`expr $TKBPS / 1024`
		echo -e "$INSTFIL4 发送 $1: \033[31m$TMBPS\033[0m mb/s 下载 \033[32m$RMBPS\033[0m mb/s "
        elif [ $RKBPS -gt 1024 ] && [ $TKBPS -lt 1024 ];then
                #RMBPS=$(printf "%.2f" `echo "scale=2;$RKBPS/1024"|bc`)
		RMBPS=`expr $RKBPS / 1024`
                echo -e "$INSTFIL4 发送 $1: \033[31m$TKBPS\033[0m kb/s 下载 \033[32m$RMBPS\033[0m mb/s "
        elif [ $RKBPS -lt 1024 ] && [ $TKBPS -gt 1024 ];then
                #TMBPS=$(printf "%.2f" `echo "scale=2;$TKBPS/1024"|bc`)
		TMBPS=`expr $TKBPS / 1024`
                echo -e "$INSTFIL4 发送 $1: \033[31m$TMBPS\033[0m mb/s 下载 \033[32m$RKBPS\033[0m kb/s "
        else
		echo -e "$INSTFIL4 发送 $1: \033[31m$TKBPS\033[0m kb/s 下载 \033[32m$RKBPS\033[0m kb/s "
	fi
        R2=`cat /sys/class/net/$1/statistics/rx_bytes`
        T2=`cat /sys/class/net/$1/statistics/tx_bytes`
        sleep 1s
done
posted @ 2020-08-01 16:10  峰哥ge  阅读(357)  评论(0编辑  收藏  举报