弱網(wǎng)測試輔助工具tc/netem pynetem

背景:

上篇文章已經(jīng)把主機(jī)弱網(wǎng)測試環(huán)境搭建好了,每次去敲命令行總是不好的。
所以選擇了pynetem輔助工具,有提供命令并且替換了api進(jìn)行服務(wù)的集成


弱網(wǎng)設(shè)置頁面集成

安裝

#只能用于linux
pip install pynetem

操作使用

有兩種模式:命令行模式、Web模式,兩種模式下每次傳入新規(guī)則都會(huì)將舊規(guī)則替換掉

  • 命令行模式
    輸入pynetem -h可以查看幫助信息
    使用實(shí)例:
# 該命令將 eth0 網(wǎng)卡的傳輸設(shè)置為 100ms ,同時(shí),大約有 30% 的包會(huì)延遲 ± 10ms 發(fā)送
pynetem -i eth0 --delay=100ms,10ms,30%

# 該命令將 eth0 網(wǎng)卡的傳輸設(shè)置為隨機(jī)丟掉 1% 的數(shù)據(jù)包,成功率為 30%
pynetem -i eth0 --loss=1%,30%

# 該命令將 eth0 網(wǎng)卡的傳輸設(shè)置為隨機(jī)產(chǎn)生 1% 的重復(fù)數(shù)據(jù)包
pynetem -i eth0 --duplicate=1%

# 該命令將 eth0 網(wǎng)卡的傳輸設(shè)置為隨機(jī)產(chǎn)生 0.2% 的損壞的數(shù)據(jù)包 。 (內(nèi)核版本需在2.6.16以上)
pynetem -i eth0 --corrupt =0.2%

# 該命令將 eth0 網(wǎng)卡的傳輸設(shè)置為:有 25% 的數(shù)據(jù)包(50%相關(guān))會(huì)被立即發(fā)送,其他的延遲 10ms
pynetem -i eth0 --delay=10ms --reorder=25%,50%

# 該命令將 eth0 網(wǎng)卡的傳輸設(shè)置為 延遲100ms,同時(shí)限定網(wǎng)卡的出速率
pynetem -i eth0 --delay=100ms --rate=256kbit

# 該命令將 eth0 網(wǎng)卡的傳輸設(shè)置為 在符合CIDR條件下延遲100ms&限定網(wǎng)卡的出速率
pynetem -i eth0 --delay=100ms --rate=256kbit --dst=10.10.10.10/32
  • Web模式
    運(yùn)行此模式可以對外提供接口,默認(rèn)開啟8899端口,也可以指定其它端口。
pynetem --web --port=9000

共有5個(gè)API可以調(diào)用:

[GET] /pynetem/help                                     -- Get demo post data and simple description
[GET] /pynetem/listInterfaces                           -- Get interfaces name of host
[GET] /pynetem/getRules?eth=<interface name>            -- Get qdisc rules by interface
[GET/DELETE] /pynetem/clear?eth=<interface name>        -- Clear all rules
[POST] /pynetem/setRules?eth=<interface name>           -- Set tc qdisc rule

請求體中,如果設(shè)置參數(shù)為None或"",則會(huì)忽略此參數(shù),請求體示例如下:

{
    "delay": "100ms 10ms 25%",
    "distribution": "normal",
    "reorder": "25% 50%",
    "loss": "0.3% 25%",
    "duplicate": "1%",
    "corrupt": "0.1%",
    "rate": "256kbit",
    "buffer": 1600,
    "limit": 3000,
    "dst": "10.10.10.0/24"
}

需要注意的是,當(dāng)按下ctrl + c關(guān)停web服務(wù)時(shí),會(huì)自動(dòng)將所有網(wǎng)卡接口下的所有qdisc規(guī)則全部清空。

請求實(shí)例:
請求參數(shù)可以參考
https://github.com/GuoTengda1993/pynetem/blob/master/pynetem/web.py

#先查看自己的網(wǎng)卡用的是哪一個(gè)
#例:我用的網(wǎng)卡是 :enp3s0
#下面是設(shè)置網(wǎng)絡(luò)忘記為延遲時(shí)間為300ms
http://192.168.24.80:9000/pynetem/setRules?eth=enp3s0
#請求體
{
    "delay": "300ms"
}
#獲取當(dāng)前的網(wǎng)絡(luò)環(huán)境
http://192.168.24.80:9000/pynetem/getRules?eth=enp3s0
#返回參數(shù)
"{"code":200,"msg":["qdisc netem 8024: root refcnt 2 limit 1000 delay 300ms"],"res":null,"status":"success"}"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容