弱網模擬tc/netem輔助工具pynetem

https://github.com/GuoTengda1993/pynetem
因為tc/netem只在linux系統(tǒng)中存在,所以只能在Linux系統(tǒng)中安裝:

pip install pynetem

使用

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

  • 命令行模式
    輸入pynetem -h可以查看幫助信息,參數和tc/netem基本一致,只使用上有些許區(qū)別,點擊此鏈接即可查看tc/netem的相關文檔:netem
    使用示例:
# 該命令將 eth0 網卡的傳輸設置為 100ms ,同時,大約有 30% 的包會延遲 ± 10ms 發(fā)送
pynetem -i eth0 --delay=100ms,10ms,30%

# 該命令將 eth0 網卡的傳輸設置為隨機丟掉 1% 的數據包,成功率為 30%
pynetem -i eth0 --loss=1%,30%

# 該命令將 eth0 網卡的傳輸設置為隨機產生 1% 的重復數據包
pynetem -i eth0 --duplicate=1%

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

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

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

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

共有5個API可以調用:

[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

請求體中,如果設置參數為None"",則會忽略此參數,請求體示例如下:

{
    "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"
}

需要注意的是,當按下ctrl + c關停web服務時,會自動將所有網卡接口下的所有qdisc規(guī)則全部清空。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容