linux 统计网卡带宽脚本

#!/bin/sh
netcard=`ip l show|grep -Ev "macvlan|ppp"|grep ens|awk -F ":" '{print $2}'`
txbyte1=0
rxbyte1=0
txbyte2=0
rxbyte2=0
for i in $netcard;do
##echo "$i ====>\n"
txbyte1=$((`ifconfig $i|grep -E "TX.+packets"|awk '{print $5}'` + $txbyte1))
rxbyte1=$((`ifconfig $i|grep -E "RX.+packets"|awk '{print $5}'` + $rxbyte1))
##echo "tx:$i: $txbyte1  rx:$i:$rxbyte1"
done
sleep 5 
for i in $netcard;do
txbyte2=$((`ifconfig $i|grep -E "TX.+packets"|awk '{print $5}'` + $txbyte2))
rxbyte2=$((`ifconfig $i|grep -E "RX.+packets"|awk '{print $5}'` + $rxbyte2))
done
txdiffbyte=$(((txbyte2 - txbyte1)/1024/1024*8/5))
rxdiffbyte=$(((rxbyte2 - rxbyte1)/1024/1024*8/5))


##echo "$rxbyte1 $rxbyte2 $rxdiffbyte MB/S"
##echo "$txbyte1 $txbyte2 $txdiffbyte MB/S"
echo "download: $rxdiffbyte MB/S  upload:$txdiffbyte MB/S"

 

posted @ 2021-06-16 19:16  嗷嗷鹿鸣[VX|dshoub]  阅读(111)  评论(0编辑  收藏  举报