小白网卡带宽限速神器: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,提升权限

博主:测试生财

座右铭:专注测试与自动化,致力提高研发效能;通过测试精进完成原始积累,通过读书理财奔向财务自由。

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

博客园:https://www.cnblogs.com/qa-freeroad/

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

猜你喜欢

转载自blog.csdn.net/ccgshigao/article/details/108396911