From 712d70396fbea5fc3abc9150035ec27f258e0cb9 Mon Sep 17 00:00:00 2001 From: NewName Date: Tue, 9 Apr 2024 01:39:47 +0800 Subject: [PATCH] Create Mbit.sh --- Mbit.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Mbit.sh diff --git a/Mbit.sh b/Mbit.sh new file mode 100644 index 0000000..7c75462 --- /dev/null +++ b/Mbit.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +GREEN='\033[0;32m' # 绿色 +NC='\033[0m' # 恢复默认颜色 + +apt update +apt install -y iproute2 + +read -p "请输入要限制带宽的端口号(多个端口用逗号分隔): " PORTS + +read -p "请输入限速值(单位为M): " LIMIT + +INTERFACE="eth0" # 要限制的网络接口 + +# 使用逗号分隔端口号 +IFS=',' read -ra PORT_ARRAY <<< "$PORTS" + +for PORT in "${PORT_ARRAY[@]}" +do + tc qdisc add dev $INTERFACE root handle 1: htb default 12 + tc class add dev $INTERFACE parent 1: classid 1:1 htb rate ${LIMIT}mbit + tc class add dev $INTERFACE parent 1:1 classid 1:12 htb rate ${LIMIT}mbit + tc filter add dev $INTERFACE protocol ip parent 1:0 prio 1 u32 match ip dport $PORT 0xffff flowid 1:12 + echo -e "${GREEN}端口 $PORT 的带宽限制已设置为 ${LIMIT}Mbit。${NC}" +done + +systemctl restart systemd-networkd