运维部落

首页 > 网络产品, 解决方案 > 使用Linux命令行测试网速Speedtest.net

使用Linux命令行测试网速Speedtest.net

2015年11月4日

当发现上网速度变慢时,人们通常会先首先测试自己的电脑到网络服务提供商(通常被称为“最后一公里”)的网络连接速度。在可用于测试宽带速度的网站中,Speedtest.net也许是使用最广泛的。

Speedtest.net的工作原理并不复杂:它在你的浏览器中加载JavaScript代码并自动检测离你最近的Speedtest.net服务器,然后向服务器发送HTTP GET and POST请求来测试上行/下行网速。

但在没有图形化桌面时(例如,当你通过命令行远程登陆服务器或使用没有图形界面的操作系统),基于flash、界面友好的Speedtest.net将无法工作。幸运的是,Speedtest.net提供了一个命令行版本——speedtest-cli。下面我将向你演示如何在Linux的命令行中使用speedtest-cli来测试宽带连接速度。

安装speedtest-cli

speedtest-cli是一个用Python编写的轻量级Linux命令行工具,在Python2.4至3.4版本下均可运行。它基于Speedtest.net的基础架构来测量网络的上/下行速率。安装speedtest-cli很简单——只需要下载其Python脚本文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@HMX1026 ~]# wget https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
--2015-11-04 21:49:14--  https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
Resolving raw.github.com... 103.245.222.133
Connecting to raw.github.com|103.245.222.133|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest_cli.py [following]
--2015-11-04 21:49:17--  https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest_cli.py
Resolving raw.githubusercontent.com... 103.245.222.133
Connecting to raw.githubusercontent.com|103.245.222.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 24994 (24K) [text/plain]
Saving to: “speedtest_cli.py”
 
100%[======================================>] 24,994      --.-K/s   in 0s
 
2015-11-04 21:49:18 (454 MB/s) - “speedtest_cli.py” saved [24994/24994]
 
[root@HMX1026 ~]# chmod a+rx speedtest_cli.py
[root@HMX1026 ~]# mv speedtest_cli.py /usr/local/bin/speedtest-cli
[root@HMX1026 ~]# chown root:root /usr/local/bin/speedtest-cli
[root@HMX1026 ~]# speedtest-cli
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from New Century InfoComm Tech Co. (123.51.171.86)...
Selecting best server based on latency...
Hosted by Asia Pacific Telecom (Taipei) [5.43 km]: 2.141 ms
Testing download speed........................................
Download: 57.70 Mbit/s
Testing upload speed..................................................
Upload: 81.65 Mbit/s

如果你愿意分享测试结果,你可以使用参数“–share”。它将会把你的测试结果上传到Speedtest.net服务器并以图形的方式分享给其他人。

1
2
3
4
5
6
7
8
9
10
11
12
[root@HMX1026 ~]# speedtest-cli --share
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from New Century InfoComm Tech Co. (123.51.171.86)...
Selecting best server based on latency...
Hosted by Asia Pacific Telecom (Taipei) [5.43 km]: 2.109 ms
Testing download speed........................................
Download: 58.23 Mbit/s
Testing upload speed..................................................
Upload: 77.47 Mbit/s
Share results: http://www.speedtest.net/result/4803520528.png
[root@HMX1026 ~]#

如果你对目前所有可用的Speedtest.net服务器感兴趣,你可以使用参数“–list”。它会打印出所有的Speedtest.net服务器(按照离你的地理距离由近及远排序)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@HMX1026 ~]# speedtest-cli --list |more
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
3967) Chief Telecom (Taipei, Taiwan) [5.43 km]
2133) Taiwan Fixed Network (Taipei, Taiwan) [5.43 km]
5056) Taipei Fiber (Taipei, Taiwan) [5.43 km]
2181) kbro Co., Ltd. (Taipei, Taiwan) [5.43 km]
5008) Asia Pacific Telecom (Taipei, Taiwan) [5.43 km]
5661) NCIC Telcom (Taipei, Taiwan) [5.43 km]
2327) Far Eastone Telecommunications Co., Ltd. (Taipei, Taiwan) [5.43 km]
2188) TFN Media Co., Ltd. (Taipei, Taiwan) [5.43 km]
5315) Taiwan Star Telecom Co., Ltd. (Taipei, Taiwan) [5.43 km]
4505) Chief Telecom (New Taipei, Taiwan) [11.92 km]
5219) Taiwan Fixed Network (New Taipei, Taiwan) [11.92 km]
5067) Far Eastone Telecommunications Co., Ltd. (New Taipei, Taiwan) [11.92 km]
5660) NCIC Telcom (New Taipei, Taiwan) [11.92 km]
5334) Taiwan Star Telecom Co., Ltd. (New Taipei, Taiwan) [11.92 km]
3921) Taiwan Fixed Network (Taoyuan, Taiwan) [31.28 km]
4938) Chief Telecom (Taoyuan, Taiwan) [31.28 km]
2589) Far EasTone Telecommunications Co., Ltd (Taoyuan, Taiwan) [31.28 km]
2187) TFN Media Co., Ltd. (Yilan, Taiwan) [49.53 km]
4947) Chief Telecom (Yilan, Taiwan) [49.53 km]
5194) Taiwan Fixed Network (Yilan, Taiwan) [49.53 km]
5068) Far Eastone Telecommunications Co., Ltd. (Yilan, Taiwan) [49.53 km]

在上面的列表中,每个服务器的前面都有一个与其对应的ID。如果想使用指定的服务器来测试你的网速,你只需要在speedtest-cli命令后指定其ID即可。例如,如果想使用在Taiwan的服务器,你只需要指定相对应的服务器ID(如5068)。

1
2
3
4
5
6
7
8
9
[root@HMX1026 ~]# speedtest-cli --server 5068
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from New Century InfoComm Tech Co. (123.51.171.86)...
Hosted by Far Eastone Telecommunications Co., Ltd. (Yilan) [49.53 km]: 4.448 ms
Testing download speed........................................
Download: 59.02 Mbit/s
Testing upload speed..................................................
Upload: 82.67 Mbit/s
本文的评论功能被关闭了.