QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4646|回复: 5

终端下显示网速的SHELL脚本

[复制链接]
发表于 2007-7-31 04:50:26 | 显示全部楼层 |阅读模式
除了视频, 终端下的生活已经很完美和效率了
( framebuffer: 喂, 我可以让你在字符界面看电影的... )


我几乎用不到X, 而且X20的配置, 开X有点虚...


more /proc/cpuifon ; free ; df ; top
可以看一些系统的状况, 比如CPU信息, 内存, 硬盘, 进程...
但是dorainm一直没有找到一款能即时显示网速的
比如 eth0上传 xx KB/S
虽然 /sbin/ifconfig 可以查看网络状态


毛主席告诉我们, 自动动手, 丰衣足食
因为 /sbin/ifconfig 里面有条信息,
告诉我们当前某个连接的总数据量 total bytes


如果我们每1秒钟去获取这个total bytes
然后把两次结果相减, 不就可以获得 xx B/S的网络速度了吗??


当然, 这个数字也会一时无法直观表示,
比如当这数字大于 1024时候, 就转化为 KB/S 的单位
如果数字大于 1024*1024, 就转化为 MB/S
... ...
这样子结果就比较直观了


okay, let's go
rate程序写了有一年了吧, google了好多拼凑出来的
代码很难阅读, 本来想什么时候优化一下, 发布出来
但是一直没有做, 今天决定还是拿出来让需要的群众来使用吧


运行
$ rate --help
usage : /home/dorainm/bin/rate [DEV_NAME] [ TIMES | -t ]


  DEV_NAME    the name of the network device, default: eth0
  TIMES       the times of counts, default: 1
  -t          counting until break
$


比如查看 lo 1次( 1秒钟 ) 的网络速度
$ rate lo
the Rate of Network,   by dorainm, [email protected]
-----------------------------------------------------
RX Rate = 767 B/S       Totle Bytes : 8.182
TX Rate = 767 B/S       Totle Bytes : 8.182
-----------------------------------------------------
All Done
$


下面看持续观看 eth0 的网络速度
$ rate -t
the Rate of Network,   by dorainm, [email protected]
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.710 MB
TX Rate = 0 B/S         Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.710 MB
TX Rate = 0 B/S         Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 60 B/S        Totle Bytes : 40.710 MB
TX Rate = 66 B/S        Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 154 B/S       Totle Bytes : 40.710 MB
TX Rate = 146 B/S       Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.710 MB
TX Rate = 0 B/S         Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 71 B/S        Totle Bytes : 40.710 MB
TX Rate = 137 B/S       Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.710 MB
TX Rate = 0 B/S         Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 140 B/S       Totle Bytes : 40.710 MB
TX Rate = 156 B/S       Totle Bytes : 30.908 MB
-----------------------------------------------------
RX Rate = 1.343 KB/S    Totle Bytes : 40.712 MB
TX Rate = 707 B/S       Totle Bytes : 30.909 MB
-----------------------------------------------------
RX Rate = 757 B/S       Totle Bytes : 40.713 MB
TX Rate = 564 B/S       Totle Bytes : 30.910 MB
-----------------------------------------------------
RX Rate = 370 B/S       Totle Bytes : 40.713 MB
TX Rate = 263 B/S       Totle Bytes : 30.910 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.713 MB
TX Rate = 0 B/S         Totle Bytes : 30.910 MB
-----------------------------------------------------
RX Rate = 256 B/S       Totle Bytes : 40.713 MB
TX Rate = 619 B/S       Totle Bytes : 30.910 MB
-----------------------------------------------------
RX Rate = 6.039 KB/S    Totle Bytes : 40.719 MB
TX Rate = 354 B/S       Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.719 MB
TX Rate = 0 B/S         Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 1.470 KB/S    Totle Bytes : 40.720 MB
TX Rate = 210 B/S       Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 66 B/S        Totle Bytes : 40.721 MB
TX Rate = 0 B/S         Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.721 MB
TX Rate = 0 B/S         Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.721 MB
TX Rate = 0 B/S         Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.721 MB
TX Rate = 0 B/S         Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 370 B/S       Totle Bytes : 40.721 MB
TX Rate = 366 B/S       Totle Bytes : 30.911 MB
-----------------------------------------------------
RX Rate = 1.417 KB/S    Totle Bytes : 40.722 MB
TX Rate = 430 B/S       Totle Bytes : 30.912 MB
-----------------------------------------------------
RX Rate = 405 B/S       Totle Bytes : 40.723 MB
TX Rate = 66 B/S        Totle Bytes : 30.918 MB
-----------------------------------------------------
RX Rate = 60 B/S        Totle Bytes : 40.723 MB
TX Rate = 128 B/S       Totle Bytes : 30.918 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.723 MB
TX Rate = 152 B/S       Totle Bytes : 30.919 MB
-----------------------------------------------------
RX Rate = 74 B/S        Totle Bytes : 40.724 MB
TX Rate = 614 B/S       Totle Bytes : 30.919 MB
-----------------------------------------------------
RX Rate = 5.904 KB/S    Totle Bytes : 40.729 MB
TX Rate = 300 B/S       Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 1.535 KB/S    Totle Bytes : 40.731 MB
TX Rate = 210 B/S       Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.731 MB
TX Rate = 0 B/S         Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.731 MB
TX Rate = 0 B/S         Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.731 MB
TX Rate = 0 B/S         Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 140 B/S       Totle Bytes : 40.731 MB
TX Rate = 156 B/S       Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.731 MB
TX Rate = 0 B/S         Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.731 MB
TX Rate = 0 B/S         Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 230 B/S       Totle Bytes : 40.731 MB
TX Rate = 210 B/S       Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.731 MB
TX Rate = 0 B/S         Totle Bytes : 30.920 MB
-----------------------------------------------------
RX Rate = 1.595 KB/S    Totle Bytes : 40.733 MB
TX Rate = 582 B/S       Totle Bytes : 30.921 MB
-----------------------------------------------------
RX Rate = 60.839 KB/S   Totle Bytes : 40.792 MB
TX Rate = 2.443 MB/S    Totle Bytes : 33.364 MB
-----------------------------------------------------
RX Rate = 0 B/S         Totle Bytes : 40.792 MB
TX Rate = 0 B/S         Totle Bytes : 33.364 MB
-----------------------------------------------------
RX Rate = 74 B/S        Totle Bytes : 40.792 MB
TX Rate = 615 B/S       Totle Bytes : 33.365 MB
-----------------------------------------------------
RX Rate = 7.312 KB/S    Totle Bytes : 40.799 MB
TX Rate = 408 B/S       Totle Bytes : 33.365 MB
-----------------------------------------------------
^c
$
 楼主| 发表于 2007-7-31 04:51:04 | 显示全部楼层
附rate源码

  1. #!/bin/bash





  2. HL_START="\033[37;40;1m"

  3. HL_END="\033[0m"

  4. DEV_NAME="eth0"





  5. function disp_usage()

  6. {

  7.         printf "usage : $1 [DEV_NAME] [ TIMES | -t ]\n\n"

  8.         printf "  DEV_NAME    the name of the network device, default: eth0\n"

  9.         printf "  TIMES       the times of counts, default: 1\n"

  10.         printf "  -t          counting until break\n"







  11. }





  12. if [ $# -eq 0 ]

  13. then

  14.         TOTAL_TIME=1

  15. elif [ $1 == "-h" ]

  16. then

  17.         disp_usage $0

  18.         exit 0

  19. elif [ $1 == "--help" ]

  20. then

  21.         disp_usage $0

  22.         exit 0

  23. elif [ $1 == "-t" ]

  24. then

  25.         TOTAL_TIME=65535

  26. elif [ $# -eq 1 ]

  27. then

  28.         DEV_NAME=$1

  29.         TOTAL_TIME=1

  30. elif [ $2 == "-t" ]

  31. then

  32.         DEV_NAME=$1

  33.         TOTAL_TIME=65535

  34. else

  35.         DEV_NAME=$1

  36.         TOTAL_TIME=$2

  37. fi



  38. echo "the Rate of Network,   by dorainm, [email][email protected][/email]"

  39. echo "-----------------------------------------------------"





  40. count=0

  41. while [ $count -lt $TOTAL_TIME ]

  42. do

  43. RX_FIRST=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $2}' | awk -F: '{print $2}')

  44. TX_FIRST=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $6}' | awk -F: '{print $2}')

  45. # echo "FIRST = $FIRST"

  46. sleep 1

  47. RX_SECOND=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $2}' | awk -F: '{print $2}')

  48. TX_SECOND=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $6}' | awk -F: '{print $2}')

  49. # echo "SECOND = $SECOND"

  50. ((RX_FIRST=$RX_SECOND-$RX_FIRST))

  51. ((TX_FIRST=$TX_SECOND-$TX_FIRST))





  52. if [ $RX_FIRST -gt 1048576 ]

  53. then

  54.         RX_FIRST=$(echo "scale=3; $RX_FIRST / 1048576" | bc -l)

  55.     printf "RX Rate = $HL_START%.3F$HL_END MB/S\t" $RX_FIRST

  56. elif [ $RX_FIRST -gt 1024 ]

  57. then

  58.         RX_FIRST=$(echo "scale=3; $RX_FIRST / 1024" | bc -l)

  59.         # ((RX_FIRST=($RX_FIRST/1024)))

  60.         printf "RX Rate = $HL_START%.3F$HL_END KB/S\t" $RX_FIRST

  61. else

  62.         printf "RX Rate = $HL_START$RX_FIRST$HL_END B/S \t"

  63. fi





  64. if [ $RX_SECOND -gt 1048576 ]

  65. then

  66.         RX_SECOND=$(echo "scale=3; $RX_SECOND / 1048576" | bc -l)

  67.         printf "Totle Bytes : $HL_START$RX_SECOND$HL_END MB\n"

  68. elif  [ $RX_SECOND -gt 1024 ]

  69. then

  70.     RX_SECOND=$(echo "scale=3; $RX_SECOND / 1024" | bc -l)

  71.     printf "Totle Bytes : $HL_START$RX_SECOND$HL_END KB\n"

  72. else

  73.     printf "Totle Bytes : $HL_START$RX_SECOND$HL_END B\n"

  74. fi





  75. if [ $TX_FIRST -gt 1048576 ]

  76. then
  77.     TX_FIRST=$(echo "scale=3; $TX_FIRST / 1048576" | bc -l)
  78.     printf "TX Rate = $HL_START%.3F$HL_END MB/S\t" $TX_FIRST
  79. elif [ $TX_FIRST -gt 1024 ]
  80. then
  81.     TX_FIRST=$(echo "scale=3; $TX_FIRST / 1024" | bc -l)
  82.     printf "TX Rate = $HL_START%.3F$HL_END KB/S\t" $TX_FIRST
  83. else
  84.     printf "TX Rate = $HL_START$TX_FIRST$HL_END B/S \t"

  85. fi

  86. if [ $TX_SECOND -gt 1048576 ]
  87. then
  88.         TX_SECOND=$(echo "scale=3; $TX_SECOND / 1048576" | bc -l)
  89.     printf "Totle Bytes : $HL_START$TX_SECOND$HL_END MB\n"
  90. elif  [ $TX_SECOND -gt 1024 ]
  91. then
  92.     TX_SECOND=$(echo "scale=3; $TX_SECOND / 1024" | bc -l)
  93.     printf "Totle Bytes : $HL_START$TX_SECOND$HL_END KB\n"
  94. else
  95.     printf "Totle Bytes : $HL_START$TX_SECOND$HL_END B\n"

  96. fi

  97. ((count=$count+1))

  98. echo "-----------------------------------------------------"

  99. done





  100. echo "All Done"
复制代码
回复

使用道具 举报

发表于 2007-7-31 09:43:27 | 显示全部楼层
谢谢。我从来没注意国ifconfig里面竟然有提示这个,一直是想用mrtg做的。不过既然知道了原理,还是自己做一个的比较好,顺便锻炼shell能力。
回复

使用道具 举报

发表于 2007-8-5 14:27:22 | 显示全部楼层
不错
回复

使用道具 举报

发表于 2009-3-22 02:06:25 | 显示全部楼层
回复

使用道具 举报

发表于 2009-8-15 11:59:54 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-2 00:32 , Processed in 0.051529 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表