centos7.4和ubuntu20.04,实时打印网卡速率脚本:
#!/bin/bash
function up_interfaces()
{
itfs=`ip a | grep 'state UP' | awk '{print $2}' | awk -F : '{print $1}'`
itfs=(${itfs// / })
echo "all state UP interfaces:"
for itf in ${itfs[@]}
do
uname -a | grep Ubuntu >> /dev/null
if [ $? -eq 0 ]; then
ip a | grep inet | grep $itf | awk '{print "\t" $8 ": " $2}'
else
ip a | grep inet | grep $itf | awk '{print "\t" $7 ": " $2}'
fi
done
}
if [ $# -lt 1 ] ; then
echo "parameters error"
echo -e "\t$0 [interface]"
echo
up_interfaces
exit
fi
interface=$1
while true
do
info_1=`cat /proc/net/dev`
nanoSec_1=`date +%s%N`
sleep 1
info_2=`cat /proc/net/dev`
nanoSec_2=`date +%s%N`
TX_1=`echo ${info_1#*$interface} | awk '{print $10}'`
RX_1=`echo ${info_1#*$interface} | awk '{print $2}'`
TX_2=`echo ${info_2#*$interface} | awk '{print $10}'`
RX_2=`echo ${info_2#*$interface} | awk '{print $2}'`
TX=$((${TX_2}-${TX_1}))
RX=$((${RX_2}-${RX_1}))
nanoSec=$((${nanoSec_2}-${nanoSec_1}))
second=`echo $nanoSec | awk '{print $1/1000000000}'`
TX=`echo "$TX $second" | awk '{printf ("%.4f\n", $1/$2/1048576*8)}'`
RX=`echo "$RX $second" | awk '{printf ("%.4f\n", $1/$2/1048576*8)}'`
echo -e "`date +%k:%M:%S` \t TX: ${TX} Mbps/s \t\t RX: ${RX} Mbps/s"
done