这是我无聊写的脚本,用于下载速度测试:
#!/bin/bash
get_ispeed() {
echo $(ifconfig eth0 | grep bytes | grep RX | cut -d ':' -f 2 | cut -d ' ' -f 1);
}
for((;;));
do
s1=`get_ispeed`;
sleep 1s;
s2=`get_ispeed`;
d=$(($s2-$s1));
echo $(($d / 1000))" kB/s";
done
Run Code Online (Sandbox Code Playgroud)
不确定它是否在“完成工作”,尽管我不是真正的 bashist :p
由于 ifconfig 产生 RX 字节,我将其除以 1000 以获得 kB/s
我不认为您真的在寻找链接速度,这是您的连接的专用连接速度。如果您已ethtool
安装(在 repos 中),您可以使用此命令来获取链接速度:
$ sudo ethtool eth0 | grep -i speed
Speed: 100Mb/s
Run Code Online (Sandbox Code Playgroud)
您想查看带宽速度。在给定的时间内您使用的总速度是多少。有两种方法可以获取eth0
接口读取的字节数:
$ cat /sys/class/net/eth0/statistics/rx_bytes
3431530366
Run Code Online (Sandbox Code Playgroud)
还有/proc/net/dev
,我相信它是提供以下数据的内核结构ifconfig
:
$ cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo:629956414 572909 0 0 0 0 0 0 629956414 572909 0 0 0 0 0 0
eth0:3431888253 329701689 0 0 0 0 0 359127 831203319 353144288 0 0 0 0 0 0
sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
更好的是使用实际工具来测量您的带宽使用情况。这里有很多选择:
使用 wget/curl
这些非常简单。选择一个大文件进行下载,并在完成后查看任一工具报告的统计信息。
$ wget --output-document=/dev/null http://speedtest.wdc01.softlayer.com/downloads/test500.zip
-or-
$ curl -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test500.zip
Run Code Online (Sandbox Code Playgroud)使用 cli 监控工具
该类别中有许多工具。这里有一些可以帮助您入门。
这从命令行使用了古老的speedtest.net网站。
$ ./speedtest-cli
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from Comcast Cable (x.x.x.x)...
Selecting best server based on ping...
Hosted by FiberCloud, Inc (Seattle, WA) [12.03 km]: 44.028 ms
Testing download speed........................................
Download: 32.29 Mbit/s
Testing upload speed..................................................
Upload: 5.18 Mbit/s
Run Code Online (Sandbox Code Playgroud)使用iperf
为此,您将设置自己的服务器和客户端,然后测量两台计算机之间的带宽性能。从某种意义上说,与依赖特定 Internet 目标的性能相比,您可以获得更准确的计算机/网络性能图,这是更好的选择。
在服务器上:
$ iperf -s
Run Code Online (Sandbox Code Playgroud)
在客户端:
$ iperf -c myserver.mydom
------------------------------------------------------------
Client connecting to 192.168.1.1, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[ 3] local 192.168.1.3 port 52143 connected with 192.168.1.1 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 113 MBytes 94.7 Mbits/sec
Run Code Online (Sandbox Code Playgroud)小智 5
我真的同意你的问题和答案,因此,我真的可以用一行来完成此操作:
m1=`cat /sys/class/net/eth0/statistics/tx_bytes` ; sleep 10s ; m2=`cat /sys/class/net/eth0/statistics/rx_bytes` ; echo $((($m2-$m1)/10240))
Run Code Online (Sandbox Code Playgroud)
而且打印得很好。
以更复杂的方式:
#!/bin/bash
intervalo=3
info="/sys/class/net/"
cd $info
for interface in eth*
do
rx1=`cat $info$interface/statistics/rx_bytes`
tx1=`cat $info$interface/statistics/tx_bytes`
`sleep $((intervalo))s`
rx2=`cat $info$interface/statistics/rx_bytes`
tx2=`cat $info$interface/statistics/tx_bytes`
echo $interface
echo ----
echo RX: $((($rx2-$rx1)/($intervalo*1024))) Kbps
echo TX: $((($tx2-$tx1)/($intervalo*1024))) Kbps
done
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但可以使用数组来保存每个接口结果来改进,因此它只需要为所有接口进行睡眠,而不需要对其中任何一个接口进行睡眠。
归档时间: |
|
查看次数: |
52940 次 |
最近记录: |