vim rx_tx.sh

    1. #!/bin/bash
    2. tmp_file=/tmp/.rx_tx.tmp
    3. tmp_log=/tmp/.rx_tx.log
    4. {
    5. while true
    6. do
    7. devs=(`cat /proc/net/dev | sed -n '/:/p' | sed 's/:/ /g' | awk '{print $1}'`)
    8. RX_array_pre=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $2}'`)
    9. TX_array_pre=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $10}'`)
    10. sleep 1
    11. pid=`ps -ef|grep $0 |grep -v grep|awk 'NR==1{print $2}'`
    12. if [[ $$ -ne $pid || -e $tmp_file ]];then
    13. rm -rf $tmp_file
    14. break;
    15. fi
    16. RX_array_next=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $2}'`)
    17. TX_array_next=(`cat /proc/net/dev | awk '/:/' | sed 's/:/ /g' | awk '{print $10}'`)
    18. for i in `seq ${#RX_array_next[@]}`
    19. do
    20. RX_array[i-1]=$((${RX_array_next[i-1]}-${RX_array_pre[i-1]}))
    21. TX_array[i-1]=$((${TX_array_next[i-1]}-${TX_array_pre[i-1]}))
    22. if [[ ${RX_array[i-1]} -lt 1024 ]];then
    23. RX_array[i-1]="${RX_array[i-1]}B/s"
    24. elif [[ ${RX_array[i-1]} -gt 1048576 ]];then
    25. RX_array[i-1]=$(echo ${RX_array[i-1]} | awk '{print $1/1048576 "MB/s"}')
    26. else
    27. RX_array[i-1]=$(echo ${RX_array[i-1]} | awk '{print $1/1024 "KB/s"}')
    28. fi
    29. if [[ ${TX_array[i-1]} -lt 1024 ]];then
    30. TX_array[i-1]="${TX_array[i-1]}B/s"
    31. elif [[ ${TX_array[i-1]} -gt 1048576 ]];then
    32. TX_array[i-1]=$(echo ${TX_array[i-1]} | awk '{print $1/1048576 "MB/s"}')
    33. else
    34. TX_array[i-1]=$(echo ${TX_array[i-1]} | awk '{print $1/1024 "KB/s"}')
    35. fi
    36. done
    37. clear
    38. echo -e "`date '+%Y-%m-%d %H:%M:%S'`\n" >> $tmp_log
    39. printf "%-15s%-15s%-15s\n" "DEV" "RX(下行)" "TX(上行)" >> $tmp_log
    40. for i in `seq ${#RX_array_next[@]}`
    41. do
    42. printf "%-15s%-15s%-15s\n" ${devs[i-1]} ${RX_array[i-1]} ${TX_array[i-1]} >> $tmp_log
    43. done
    44. cat $tmp_log
    45. > $tmp_log
    46. echo -n -e "\n(q for quit): "
    47. done
    48. } &
    49. while true
    50. do
    51. read flag
    52. if [[ "$flag" = "q" ]];then
    53. touch $tmp_file
    54. break;
    55. fi
    56. done