天天看點

小白網卡帶寬限速神器:wondershaper

介紹

WonderShaper是一個用于對特定網卡進行帶寬限速的腳本,它封裝了linux的tc指令,但是使用起來更加簡單和友善,如果你對限速要求不多,可以用wondershaper滿足你大部分需求。

項目位址: https://github.com/magnific0/wondershaper.git

使用場景

像有些做流量控制的軟體都會涉及到此類限速場景,比如測試一些離線上傳功能,在網卡帶寬較小的情況下,是否能夠優雅處理完所有離線資料上報。

弱網測試,比如手機連接配接linux機器的共享網絡,可以通過這種方式限速做弱網測試

安裝

Ubuntu:

sudo apt-get update
sudo apt install -y wondersharper      

Centos

yum install epel-release -y
sudo yum install -y wondersharper      

使用二進制

1)下載下傳wondershaper工程

git clone https://github.com/magnific0/wondershaper.git
cd wondershaper 
#直接運作wondershaper
./wondershaper      

2)全局安裝wondershaper(可選)

cd wondershaper
sudo make install      

幫助

輸入:./wondershaper -h

USAGE: ./wondershaper [-hcs] [-a <adapter>] [-d <rate>] [-u <rate>]
 
Limit the bandwidth of an adapter
 
OPTIONS:
-h Show this message                                 //顯示幫助資訊
-a <adapter> Set the adapter                         //指定網卡接口,通過ifconifg或者ip show addr檢視:比如eth0
-d <rate> Set maximum download rate (in Kbps) and/or //限制下載下傳的速度
-u <rate> Set maximum upload rate (in Kbps)          //限制上傳的速度
-p Use presets in "/etc/systemd/wondershaper.conf"
-c Clear the limits from adapter                     //清除對應網卡接口的規則
-s Show the current status of adapter                //顯示目前網卡狀态
-v Show the current version                          //顯示版本資訊
 
Configure HIPRIODST in "/etc/systemd/wondershaper.conf" for hosts
requiring high priority i.e. in case ssh uses dport 443.
 
MODES:
wondershaper -a <adapter> -d <rate> -u <rate>
wondershaper -c -a <adapter>
wondershaper -s -a <adapter>
 
EXAMPLES: //針對以上示例給出的example
wondershaper -a eth0 -d 1024 -u 512    //限制eth0上行速度512kbps,下行速度1024kbps
wondershaper -a eth0 -u 512            //僅限制eth0上行速度512kbps
wondershaper -c -a eth0                //清除為eth0設定的wondershaper的相關規則      

例子

1.限制eth0的上行帶寬為100Mbps,下行帶寬為100Mbps

wondershaper -a eth1 -d 94000 -u 94000

2.限制eth0的上行帶寬為100Mbps

wondershaper -a eth1 -u 94000

3.清理eth0上的帶寬限制規則

wondershaper -c -a eth0

4.檢視eth0目前的狀态

wondershaper -s -a eth0

5.使用systemd管理wondershaper

1) 安裝到系統目錄:sudo make install

如圖所示,wondershaper一鍵被添加到systemd進行管理了。

2)相關操作

#啟動限流操作
service wondershaper start
#停止限流操作
service wondershaper stop      

3)設定相關規則

sudo vim /etc/systemd/wondershaper.conf,修改如下内容:

[wondershaper]
 
# Adapter
IFACE="eth0" //相當于指令中的-a對應值設定
 
# Download rate in Kbps
DSPEED="2048" //相當于指令中的-d對應值設定
 
# Upload rate in Kbps
USPEED="512" //相當于指令中的-u對應值設定      

FAQ

執行指令報錯:Operation not permitted 

指令加上sudo,提升權限

部落客:測試生财(一個不為996而996的測開碼農)

座右銘:專注測試開發與自動化運維,努力讀書思考寫作,為内卷的人生奠定财務自由。

内容範疇:技術提升,職場雜談,事業發展,閱讀寫作,投資理财,健康人生。

csdn:https://blog.csdn.net/ccgshigao

部落格園:https://www.cnblogs.com/qa-freeroad/

51cto:https://blog.51cto.com/14900374

微信公衆号:測試生财(定期分享獨家内容和資源)

小白網卡帶寬限速神器:wondershaper

繼續閱讀