vim rx_tx.sh
#!/bin/bash
tmp_file=/tmp/.rx_tx.tmp
tmp_log=/tmp/.rx_tx.log
{
while true
do
devs=(`cat /proc/net/dev | sed -n '/:/p' | sed 's/:/ /g' | awk '{print $1}'`)
RX_array_pre=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $2}'`)
TX_array_pre=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $10}'`)
sleep 1
pid=`ps -ef|grep $0 |grep -v grep|awk 'NR==1{print $2}'`
if [[ $$ -ne $pid || -e $tmp_file ]];then
rm -rf $tmp_file
break;
fi
RX_array_next=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $2}'`)
TX_array_next=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $10}'`)
for i in `seq ${#RX_array_next[@]}`
do
RX_array[i-1]=$((${RX_array_next[i-1]}-${RX_array_pre[i-1]}))
TX_array[i-1]=$((${TX_array_next[i-1]}-${TX_array_pre[i-1]}))
if [[ ${RX_array[i-1]} -lt 1024 ]];then
RX_array[i-1]="${RX_array[i-1]}B/s"
elif [[ ${RX_array[i-1]} -gt 1048576 ]];then
RX_array[i-1]=$(echo ${RX_array[i-1]} | awk '{print $1/1048576 "MB/s"}')
else
RX_array[i-1]=$(echo ${RX_array[i-1]} | awk '{print $1/1024 "KB/s"}')
fi
if [[ ${TX_array[i-1]} -lt 1024 ]];then
TX_array[i-1]="${TX_array[i-1]}B/s"
elif [[ ${TX_array[i-1]} -gt 1048576 ]];then
TX_array[i-1]=$(echo ${TX_array[i-1]} | awk '{print $1/1048576 "MB/s"}')
else
TX_array[i-1]=$(echo ${TX_array[i-1]} | awk '{print $1/1024 "KB/s"}')
fi
done
clear
echo -e "`date '+%Y-%m-%d %H:%M:%S'`\n" >> $tmp_log
printf "%-15s%-15s%-15s\n" "DEV" "RX(下行)" "TX(上行)" >> $tmp_log
for i in `seq ${#RX_array_next[@]}`
do
printf "%-15s%-15s%-15s\n" ${devs[i-1]} ${RX_array[i-1]} ${TX_array[i-1]} >> $tmp_log
done
cat $tmp_log
> $tmp_log
echo -n -e "\n(q for quit): "
done
} &
while true
do
read flag
if [[ "$flag" = "q" ]];then
touch $tmp_file
break;
fi
done