如何在Linux的終端測試網(wǎng)速
學(xué)習(xí)如何在 Linux 終端使用命令行工具 speedtest
測試網(wǎng)速,或者僅用一條 python 命令立刻獲得網(wǎng)速的測試結(jié)果。
我們都會在連接到一個新的網(wǎng)絡(luò)或者 WIFI 的時候去測試網(wǎng)絡(luò)帶寬。 為什么不用我們自己的服務(wù)器!下面將會教你如何在 Linux 終端測試網(wǎng)速。
我們多數(shù)都會使用 Ookla 的 Speedtest 來測試網(wǎng)速。 這在桌面上是很簡單的操作,訪問他們的網(wǎng)站點擊“Go”瀏覽即可。它將使用最近的服務(wù)器來掃描你的本地主機來測試網(wǎng)速。 如果你使用的是移動設(shè)備,他們有對應(yīng)的移動端 APP。但如果你使用的是只有命令行終端,界面的則會有些不同。下面讓我們一起看看如何在Linux的終端來測試網(wǎng)速。
如果你只是想偶爾的做一次網(wǎng)速測試而不想去下載測試工具,那么請往下看如何使用命令完成測試。
第一步:下載網(wǎng)速測試命令行工具。
首先,你需要從 GitHub 上下載 speedtest
命令行工具。現(xiàn)在,它也被包含在許多其它的 Linux 倉庫中,如果已經(jīng)在你的庫中,你可以直接在你的 Linux 發(fā)行版上進行安裝。
讓我們繼續(xù)下載和安裝過程,安裝的 git 包取決于你的 Linux 發(fā)行版。然后按照下面的方法來克隆 Github speedtest 存儲庫
[root@kerneltalks ~]# git clone https://github.com/sivel/speedtest-cli.git
Cloning into 'speedtest-cli'...
remote: Counting objects: 913, done.
remote: Total 913 (delta 0), reused 0 (delta 0), pack-reused 913
Receiving objects: 100% (913/913), 251.31 KiB | 143.00 KiB/s, done.
Resolving deltas: 100% (518/518), done.
它將會被克隆到你當(dāng)前的工作目錄,新的名為 speedtest-cli
的目錄將會被創(chuàng)建,你將在新的目錄下看到如下的文件。
[root@kerneltalks ~]# cd speedtest-cli
[root@kerneltalks speedtest-cli]# ll
total 96
-rw-r--r--. 1 root root 1671 Oct 7 16:55 CONTRIBUTING.md
-rw-r--r--. 1 root root 11358 Oct 7 16:55 LICENSE
-rw-r--r--. 1 root root 35 Oct 7 16:55 MANIFEST.in
-rw-r--r--. 1 root root 5215 Oct 7 16:55 README.rst
-rw-r--r--. 1 root root 20 Oct 7 16:55 setup.cfg
-rw-r--r--. 1 root root 3196 Oct 7 16:55 setup.py
-rw-r--r--. 1 root root 2385 Oct 7 16:55 speedtest-cli.1
-rw-r--r--. 1 root root 1200 Oct 7 16:55 speedtest_cli.py
-rwxr-xr-x. 1 root root 47228 Oct 7 16:55 speedtest.py
-rw-r--r--. 1 root root 333 Oct 7 16:55 tox.ini
名為 speedtest.py
的 Python 腳本文件就是用來測試網(wǎng)速的。
你可以將這個腳本鏈接到 /usr/bin
下,以便這臺機器上的所有用戶都能使用。或者你可以為這個腳本創(chuàng)建一個命令別名,這樣就能讓所有用戶很容易使用它。
運行 Python 腳本
現(xiàn)在,直接運行這個腳本,不需要添加任何參數(shù),它將會搜尋最近的服務(wù)器來測試你的網(wǎng)速。
[root@kerneltalks speedtest-cli]# python speedtest.py
Retrieving speedtest.net configuration...
Testing from Amazon (35.154.184.126)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Spectra (Mumbai) [1.15 km]: 8.174 ms
Testing download speed................................................................................
Download: 548.13 Mbit/s
Testing upload speed................................................................................................
Upload: 323.95 Mbit/s
Oh! 不要被這個網(wǎng)速驚訝道。我在 AWE EX2 的服務(wù)器上。那是亞馬遜數(shù)據(jù)中心的網(wǎng)速!
這個腳本可以添加有不同的選項。
下面的幾個選項對這個腳本可能會很有用處:
要搜尋你附近的網(wǎng)路測試服務(wù)器,使用 --list
和 grep
加上地名來列出所有附近的服務(wù)器。
[root@kerneltalks speedtest-cli]# python speedtest.py --list | grep -i mumbai
2827) Bharti Airtel Ltd (Mumbai, India) [1.15 km]
8978) Spectra (Mumbai, India) [1.15 km]
4310) Hathway Cable and Datacom Ltd (Mumbai, India) [1.15 km]
3315) Joister Broadband (Mumbai, India) [1.15 km]
1718) Vodafone India (Mumbai, India) [1.15 km]
6454) YOU Broadband India Pvt Ltd. (Mumbai, India) [1.15 km]
9764) Railtel Corporation of india Ltd (Mumbai, India) [1.15 km]
9584) Sheng Li Telecom (Mumbai, India) [1.15 km]
7605) Idea Cellular Ltd. (Mumbai, India) [1.15 km]
8122) Sify Technologies Ltd (Mumbai, India) [1.15 km]
9049) I-ON (Mumbai, India) [1.15 km]
6403) YOU Broadband India Pvt Ltd., Mumbai (Mumbai, India) [1.15 km]
然后你就能從搜尋結(jié)果中看到,第一列是服務(wù)器識別號,緊接著是公司的名稱和所在地,最后是離你的距離。
如果要使用指定的服務(wù)器來測試網(wǎng)速,后面跟上 --server
加上服務(wù)器的識別號。
[root@kerneltalks speedtest-cli]# python speedtest.py --server 2827
Retrieving speedtest.net configuration...
Testing from Amazon (35.154.184.126)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Bharti Airtel Ltd (Mumbai) [1.15 km]: 13.234 ms
Testing download speed................................................................................
Download: 93.47 Mbit/s
Testing upload speed................................................................................................
Upload: 69.25 Mbit/s
如果想得到你的測試結(jié)果的分享鏈接,使用 --share
,你將會得到測試結(jié)果的鏈接。
[root@kerneltalks speedtest-cli]# python speedtest.py --share
Retrieving speedtest.net configuration...
Testing from Amazon (35.154.184.126)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Spectra (Mumbai) [1.15 km]: 7.471 ms
Testing download speed................................................................................
Download: 621.00 Mbit/s
Testing upload speed................................................................................................
Upload: 367.37 Mbit/s
Share results: http://www.speedtest.net/result/6687428141.png
輸出中的最后一行就是你的測試結(jié)果的鏈接。下載下來的圖片內(nèi)容如下 :
這就是全部的過程!如果你不想了解這些技術(shù)細節(jié),你也可以使用如下的一行命令迅速測出你的網(wǎng)速。
要想在終端使用一條命令測試網(wǎng)速。
我們將使用 curl
工具來在線抓取上面使用的 Python 腳本然后直接用 Python 執(zhí)行腳本。
[root@kerneltalks ~]# curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
上面的腳本將會運行腳本輸出結(jié)果到屏幕上。
[root@kerneltalks speedtest-cli]# curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
Retrieving speedtest.net configuration...
Testing from Amazon (35.154.184.126)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Spectra (Mumbai) [1.15 km]: 12.599 ms
Testing download speed................................................................................
Download: 670.88 Mbit/s
Testing upload speed................................................................................................
Upload: 355.84 Mbit/s
這是在 RHEL 7 上執(zhí)行的結(jié)果,在 Ubuntu、Debian、Fedora 或者 CentOS 上一樣可以執(zhí)行。