介紹
WonderShaper是一個用於對特定網卡進行帶寬限速的腳本,它封裝了linux的tc命令,但是使用起來更加簡單和方便,如果你對限速要求不多,可以用wondershaper滿足你大部分需求。
使用場景
像有些做流量控制的軟件都會涉及到此類限速場景,比如測試一些離線上傳功能,在網卡帶寬較小的情況下,是否能夠優雅處理完所有離線數據上報。
弱網測試,比如手機連接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
微信公眾號:測試生財(定期分享獨家內容和資源)