bash看网速
时间:2010-11-16 来源:donvan
虽然写的很烂,还是留个纪念吧。。。。
#!/bin/bash
#================================================================
#Script Name: netspeed.sh
#Description: to see every netdev's speed (B/s)
#Date: 2010.11.15
#================================================================
#----------------------------------------------------------------
#Function get the value of select netdev's special type
#$1:accounts of lines
#$2:volunm of special type value
#----------------------------------------------------------------
function getvalue()
{
local i;
for ((i=0;i<=(($1-2));i++))
do
r[$i]=`sed -n ''"$((i+3))"'p' /proc/net/dev|sed 's/^ *//g'|awk -F ': *| *' '{print $'"$2"'}'`;
t[$i]=`sed -n ''"$((i+3))"'p' /proc/net/dev|sed 's/^ *//g'|awk -F ': *| *' '{print $'"$(($2+8))"'}'`;
if [ "$j" -eq 2 ]
then
name[$i]=`sed -n ''"$((i+3))"'p' /proc/net/dev|sed 's/^ *//g'|awk -F ': *| *' '{print $1}'`
fi
done
}
i=`more /proc/net/dev|wc -l` for ((j=2;j<=9;j++)) do getvalue $i $j; ro=(${ro[@]} ${r[@]}); to=(${to[@]} ${t[@]}); done sleep 1 for ((j=2;j<=9;j++)) do getvalue $i $j; rn=(${rn[@]} ${r[@]}); tn=(${tn[@]} ${t[@]}); done for ((k=0;k<((i-2))*16;k++)) do ((rn[k]=rn[k]-ro[k])) ((tn[k]=tn[k]-to[k])) done echo -e \\t\\t\\trecieved\\t\\t\\tsent echo -e Interface\\tKByte/s\\t\\tpacket/s\\tKByte/s\\t\\tpacket/s for ((j=0;j<((i-2));j++)) do rn[$j]=`echo $"((${rn[j]}/1024))"|bc -l|cut -b1-7` tn[$j]=`echo $"((${tn[j]}/1024))"|bc -l|cut -b1-7` echo -e ${name[$j]}\\t\\t${rn[$j]}\\t\\t${rn[((j+i-2))]}\\t\\t${tn[$j]}\\t\\t${tn[((j+i-2))]} done
i=`more /proc/net/dev|wc -l` for ((j=2;j<=9;j++)) do getvalue $i $j; ro=(${ro[@]} ${r[@]}); to=(${to[@]} ${t[@]}); done sleep 1 for ((j=2;j<=9;j++)) do getvalue $i $j; rn=(${rn[@]} ${r[@]}); tn=(${tn[@]} ${t[@]}); done for ((k=0;k<((i-2))*16;k++)) do ((rn[k]=rn[k]-ro[k])) ((tn[k]=tn[k]-to[k])) done echo -e \\t\\t\\trecieved\\t\\t\\tsent echo -e Interface\\tKByte/s\\t\\tpacket/s\\tKByte/s\\t\\tpacket/s for ((j=0;j<((i-2));j++)) do rn[$j]=`echo $"((${rn[j]}/1024))"|bc -l|cut -b1-7` tn[$j]=`echo $"((${tn[j]}/1024))"|bc -l|cut -b1-7` echo -e ${name[$j]}\\t\\t${rn[$j]}\\t\\t${rn[((j+i-2))]}\\t\\t${tn[$j]}\\t\\t${tn[((j+i-2))]} done
相关阅读 更多 +