| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2408 人关注过本帖
标题:终端下显示网速的SHELL脚本
只看楼主 加入收藏
dorainm
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-7-31
收藏
 问题点数:0 回复次数:1 
终端下显示网速的SHELL脚本
除了视频, 终端下的生活已经很完美和效率了
( 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, dora...@gmail.com
-----------------------------------------------------
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, dora...@gmail.com
-----------------------------------------------------
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, dora...@gmail.com"
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"

搜索更多相关主题的帖子: 网速 SHELL 终端 脚本 毛主席 
2007-07-31 02:51
可可熊
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:553
专家分:0
注 册:2007-6-15
收藏
得分:0 
这样测试似乎不是很准 确的,而且使用ifconfig已经很明显了。
2007-08-01 00:15
快速回复:终端下显示网速的SHELL脚本
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021921 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved