实现原理:通过每秒读取一次/proc/net/dev动态文件,计算得出平局速度。执行格式

    1. sh ./speedmonitor.sh eth0

    脚本详情:

    1. #!/bin/bash
    2. ethn=$1
    3. while true; do
    4. RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
    5. TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
    6. sleep 1
    7. RX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
    8. TX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
    9. clear
    10. echo -e "\t RX $(date +%k:%M:%S) TX"
    11. RX=$((${RX_next} - ${RX_pre}))
    12. TX=$((${TX_next} - ${TX_pre}))
    13. if [[ $RX -lt 1024 ]]; then
    14. RX="${RX}B/s"
    15. elif [[ $RX -gt 1048576 ]]; then
    16. RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
    17. else
    18. RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
    19. fi
    20. if [[ $TX -lt 1024 ]]; then
    21. TX="${TX}B/s"
    22. elif [[ $TX -gt 1048576 ]]; then
    23. TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
    24. else
    25. TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
    26. fi
    27. echo -e "$ethn \t $RX $TX "
    28. done