文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>终端下显示网速的SHELL脚本

终端下显示网速的SHELL脚本

时间:2007-06-16  来源:dorainm



除了视频, 终端下的生活已经很完美和效率了
( 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
$


附rate源码


#!/bin/bash

HL_START="\033[37;40;1m"
HL_END="\033[0m"
DEV_NAME="eth0"

function disp_usage()
{
        printf "usage : $1 [DEV_NAME] [ TIMES | -t ]\n\n"
        printf " DEV_NAME the name of the network device, default: eth0\n"
        printf " TIMES the times of counts, default: 1\n"
        printf " -t counting until break\n"

}

if [ $# -eq 0 ]
then
        TOTAL_TIME=1
elif [ $1 == "-h" ]
then
        disp_usage $0
        exit 0
elif [ $1 == "--help" ]
then
        disp_usage $0
        exit 0
elif [ $1 == "-t" ]
then
        TOTAL_TIME=65535
elif [ $# -eq 1 ]
then
        DEV_NAME=$1
        TOTAL_TIME=1
elif [ $2 == "-t" ]
then
        DEV_NAME=$1
        TOTAL_TIME=65535
else
        DEV_NAME=$1
        TOTAL_TIME=$2
fi

echo "the Rate of Network, by dorainm, [email protected]"
echo "-----------------------------------------------------"

count=0
while [ $count -lt $TOTAL_TIME ]
do
RX_FIRST=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $2}' | awk -F: '{print $2}')
TX_FIRST=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $6}' | awk -F: '{print $2}')
# echo "FIRST = $FIRST"
sleep 1
RX_SECOND=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $2}' | awk -F: '{print $2}')
TX_SECOND=$(/sbin/ifconfig $DEV_NAME | awk '/RX bytes/{print $6}' | awk -F: '{print $2}')
# echo "SECOND = $SECOND"
((RX_FIRST=$RX_SECOND-$RX_FIRST))
((TX_FIRST=$TX_SECOND-$TX_FIRST))

if [ $RX_FIRST -gt 1048576 ]
then
        RX_FIRST=$(echo "scale=3; $RX_FIRST / 1048576" | bc -l)
    printf "RX Rate = $HL_START%.3F$HL_END MB/S\t" $RX_FIRST
elif [ $RX_FIRST -gt 1024 ]
then
        RX_FIRST=$(echo "scale=3; $RX_FIRST / 1024" | bc -l)
        # ((RX_FIRST=($RX_FIRST/1024)))
        printf "RX Rate = $HL_START%.3F$HL_END KB/S\t" $RX_FIRST
else
        printf "RX Rate = $HL_START$RX_FIRST$HL_END B/S \t"
fi

if [ $RX_SECOND -gt 1048576 ]
then
        RX_SECOND=$(echo "scale=3; $RX_SECOND / 1048576" | bc -l)
        printf "Totle Bytes : $HL_START$RX_SECOND$HL_END MB\n"
elif [ $RX_SECOND -gt 1024 ]
then
    RX_SECOND=$(echo "scale=3; $RX_SECOND / 1024" | bc -l)
    printf "Totle Bytes : $HL_START$RX_SECOND$HL_END KB\n"
else
    printf "Totle Bytes : $HL_START$RX_SECOND$HL_END B\n"
fi

if [ $TX_FIRST -gt 1048576 ]
then
    TX_FIRST=$(echo "scale=3; $TX_FIRST / 1048576" | bc -l)
    printf "TX Rate = $HL_START%.3F$HL_END MB/S\t" $TX_FIRST
elif [ $TX_FIRST -gt 1024 ]
then
    TX_FIRST=$(echo "scale=3; $TX_FIRST / 1024" | bc -l)
    printf "TX Rate = $HL_START%.3F$HL_END KB/S\t" $TX_FIRST
else
    printf "TX Rate = $HL_START$TX_FIRST$HL_END B/S \t"
fi

if [ $TX_SECOND -gt 1048576 ]
then
        TX_SECOND=$(echo "scale=3; $TX_SECOND / 1048576" | bc -l)
    printf "Totle Bytes : $HL_START$TX_SECOND$HL_END MB\n"
elif [ $TX_SECOND -gt 1024 ]
then
    TX_SECOND=$(echo "scale=3; $TX_SECOND / 1024" | bc -l)
    printf "Totle Bytes : $HL_START$TX_SECOND$HL_END KB\n"
else
    printf "Totle Bytes : $HL_START$TX_SECOND$HL_END B\n"
fi

((count=$count+1))
echo "-----------------------------------------------------"
done

echo "All Done"

排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载