EveryDay-Shell之"ifconfig输出详解“——netspeed.sh网速监控脚本
2014-06-27 22:44
387 查看
对应文件中,比如p1p1设备对应目录:
$cd /sys/class/net/p1p1/statistics
$ls -l
-r--r--r-- 1 root root 4096 2月 4 14:35 collisions
-r--r--r-- 1 root root 4096 2月 4 14:35 multicast
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_bytes
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_compressed
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_crc_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_dropped
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_fifo_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_frame_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_length_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_missed_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_over_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_packets
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_aborted_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_bytes
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_carrier_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_compressed
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_dropped
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_fifo_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_heartbeat_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_packets
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_window_errors
$cat rx_bytes(接收总数据大小B为单位,即下行)
226698240
$cat tx_bytes(发送总数据打包B为单位,即上行)
10534137
根据这些文件存储的数据,可以设计一个监控网速的shell脚本:
#!/usr/bin/env bash
# Program: netspeed.sh
# Author: LeslieChu
# Date: 2012-02-04
# Version: 1.0
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
if [ -z "$1" ];
then
echo
echo "Usage: $0 eth0";
exit 1
fi
while true;
do
R_1=`cat /sys/class/net/$1/statistics/rx_bytes`;
T_1=`cat /sys/class/net/$1/statistics/tx_bytes`;
sleep 1;
R_2=`cat /sys/class/net/$1/statistics/rx_bytes`;
T_2=`cat /sys/class/net/$1/statistics/tx_bytes`;
R_Byte_PerSec=$(expr $R_2 - $R_1);
T_Byte_PerSec=$(expr $T_2 - $T_1);
R_KB_PerSec=$(expr ${R_Byte_PerSec} / 1024); #记得“/”左右需要空格,否则无效
T_KB_PerSec=$(expr ${T_Byte_PerSec} / 1024);
R_MB_PerSec=$(expr ${R_KB_PerSec} / 1024);
T_MB_PerSec=$(expr ${T_KB_PerSec} / 1024);
echo -en "$1->\tTX:\t${T_KB_PerSec} KB/s, \t${T_MB_PerSec}MB/s; "
echo -e " \tRX:\t${R_KB_PerSec}KB/s, \t${R_MB_PerSec}MB/s."
done
执行结果:
$./netspeed.sh
Usage: ./netspeed.sh eth0
$./netspeed.sh p1p1
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 3 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 8 KB/s, 0MB/s; RX: 20KB/s, 0MB/s.
p1p1-> TX: 10 KB/s, 0MB/s; RX: 21KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 5 KB/s, 0MB/s; RX: 1KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 3KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 1KB/s, 0MB/s.
^C
$cd /sys/class/net/p1p1/statistics
$ls -l
-r--r--r-- 1 root root 4096 2月 4 14:35 collisions
-r--r--r-- 1 root root 4096 2月 4 14:35 multicast
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_bytes
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_compressed
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_crc_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_dropped
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_fifo_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_frame_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_length_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_missed_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_over_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 rx_packets
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_aborted_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_bytes
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_carrier_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_compressed
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_dropped
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_fifo_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_heartbeat_errors
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_packets
-r--r--r-- 1 root root 4096 2月 4 14:35 tx_window_errors
$cat rx_bytes(接收总数据大小B为单位,即下行)
226698240
$cat tx_bytes(发送总数据打包B为单位,即上行)
10534137
根据这些文件存储的数据,可以设计一个监控网速的shell脚本:
#!/usr/bin/env bash
# Program: netspeed.sh
# Author: LeslieChu
# Date: 2012-02-04
# Version: 1.0
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
if [ -z "$1" ];
then
echo
echo "Usage: $0 eth0";
exit 1
fi
while true;
do
R_1=`cat /sys/class/net/$1/statistics/rx_bytes`;
T_1=`cat /sys/class/net/$1/statistics/tx_bytes`;
sleep 1;
R_2=`cat /sys/class/net/$1/statistics/rx_bytes`;
T_2=`cat /sys/class/net/$1/statistics/tx_bytes`;
R_Byte_PerSec=$(expr $R_2 - $R_1);
T_Byte_PerSec=$(expr $T_2 - $T_1);
R_KB_PerSec=$(expr ${R_Byte_PerSec} / 1024); #记得“/”左右需要空格,否则无效
T_KB_PerSec=$(expr ${T_Byte_PerSec} / 1024);
R_MB_PerSec=$(expr ${R_KB_PerSec} / 1024);
T_MB_PerSec=$(expr ${T_KB_PerSec} / 1024);
echo -en "$1->\tTX:\t${T_KB_PerSec} KB/s, \t${T_MB_PerSec}MB/s; "
echo -e " \tRX:\t${R_KB_PerSec}KB/s, \t${R_MB_PerSec}MB/s."
done
执行结果:
$./netspeed.sh
Usage: ./netspeed.sh eth0
$./netspeed.sh p1p1
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 3 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 8 KB/s, 0MB/s; RX: 20KB/s, 0MB/s.
p1p1-> TX: 10 KB/s, 0MB/s; RX: 21KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 5 KB/s, 0MB/s; RX: 1KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 3KB/s, 0MB/s.
p1p1-> TX: 0 KB/s, 0MB/s; RX: 1KB/s, 0MB/s.
^C
相关文章推荐
- asp.net后台长时间操作时,向前台输出“请等待"信息的方法
- Shell脚本中滤掉"ssh -t"执行命令时返回的/r字符
- 在ASP.NET中使用ContentType="类型名";来确定输出格式
- shell 脚本 正则表达式通配符之 "[]" [大三TJB_708]
- 一些shell脚本中,为什么要用 x""==x"abc"的格式进行字符串比较
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- Asp.net出错提示"无法找到脚本"/aspnet_client/syste..."解决办法
- Shell 输出字体颜色与背景颜色格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m"
- MVC详解:了解真正所谓的"框架"(http://vipnews.csdn.net/newscontent.aspx?pointid=2009_02_01_144216610)
- 解决:CS0016: 未能写入输出文件 "....Framework\v4.0.30319\Temporary ASP.NET Files..."拒绝访问。
- 关于Shell 脚本中的"[: too many arguments"错误
- shell脚本报错:"[: =: unary operator expected"
- 运行ASP.NET出现的问题:无法直接启动带有"类型输出类型"的项目
- cs0016:未能写入输出文件 "c:"WINDOWS"Microsoft.NET"Framework"v2.0.50727"Temporary ASP.NET Files"root"...."*.dll--“拒绝访问”
- shell脚本报错:"[: =: unary operator expected"
- linux下shell 网速监控脚本
- shell脚本中的"2< " ">&2" "&>"
- shell脚本----调试脚本时候出现Syntax error: "(" unexpected错误解决
- 在ASP.NET中使用Response.ContentType="类型名";来确定输出格式