linux之实时监控网卡流量

3年前 (2017-07-22) gtj linux, shell 0评论 已收录 171℃

脚本如下

#!/bin/bash
 while [ "1" ]
 do
 eth=$1
 RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
 TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
 sleep 1
 RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
 TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
 clear
 echo -e "t RX `date +%k:%M:%S` TX"
 RX=$((${RXnext}-${RXpre}))
 TX=$((${TXnext}-${TXpre}))
 if [[ $RX -lt 1024 ]];then
 RX="${RX}B/s"
 elif [[ $RX -gt 1048576 ]];then
 RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
 else
 RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
 fi
if [[ $TX -lt 1024 ]];then
 TX="${TX}B/s"
 elif [[ $TX -gt 1048576 ]];then
 TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
 else
 TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
 fi
 echo -e "$eth t $RX $TX "
 done

用法:

[root@sf106232 ~]# sh check_eth.sh eth0

结果:

t RX 19:13:36 TX
eth0 t 60B/s 0B/s
t RX 19:13:38 TX
eth0 t 60B/s 0B/s

博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!