Bash 脚本 - 流量整形

Bash 脚本 - 流量整形

大家好,我想知道你是否可以看看我的脚本并帮我添加一些内容,

  1. 如何让它找到我有多少个活动以太网端口?以及如何过滤超过 1 个以太网端口
  2. 我如何得到这个 IP 地址范围?
  3. 一旦我有了几个以太网端口,我就需要为每个端口添加流量控制

#!/bin/bash

# Name of the traffic control command.
TC=/sbin/tc

# The network interface we're planning on limiting bandwidth.

IF=eth0 # Network card interface

# Download limit (in mega bits)
DNLD=10mbit # DOWNLOAD Limit

# Upload limit (in mega bits)
UPLD=1mbit # UPLOAD Limit

# IP address range of the machine we are controlling
IP=192.168.0.1 # Host IP

# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"

start() {

    # Hierarchical Token Bucket (HTB) to shape bandwidth

    $TC qdisc add dev $IF root handle 1: htb default 30 #Creates the root schedlar
    $TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD #Creates a child schedlar to shape download
    $TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD #Creates a child schedlar to shape upload
    $U32 match ip dst $IP/24 flowid 1:1 #Filter to match the interface, limit download speed
    $U32 match ip src $IP/24 flowid 1:2 #Filter to match the interface, limit upload speed
}


stop() {

    # Stop the bandwidth shaping.
    $TC qdisc del dev $IF root

}

restart() {

    # Self-explanatory.
    stop
    sleep 1
    start

}

show() {

    # Display status of traffic control status.
    $TC -s qdisc ls dev $IF

}

case "$1" in

    start)

        echo -n "Starting bandwidth shaping: "
        start
        echo "done"
        ;;

    stop)

        echo -n "Stopping bandwidth shaping: "
        stop
        echo "done"
        ;;

    restart)

        echo -n "Restarting bandwidth shaping: "
        restart
        echo "done"
        ;;

    show)

        echo "Bandwidth shaping status for $IF:"
        show
        echo ""
        ;;

    *)

        pwd=$(pwd)
        echo "Usage: tc.bash {start|stop|restart|show}"
        ;;

esac

exit 0

谢谢

答案1

如果它们尚未重命名,您可以通过查找匹配的目录来查找所有以太网设备eth*/sys/class/net/要将这些操作应用于多个 NIC 和 IP 地址,请查看for 循环. 以下是一个例子

#!/bin/bash  

ADDRESSES="192.0.2.1 192.0.2.2"  

for I in /sys/class/net/eth*  
do  
    I=$(basename $I)  
    for A in $ADDRESSES  
    do  
        echo $I $A  
    done  
done

在具有两个以太网设备的系统上,这将输出

eth0 192.0.2.1
eth0 192.0.2.2
eth1 192.0.2.1
eth1 192.0.2.2

答案2

获取以太网链接列表:

/sbin/ip link

获取已连接的以太网链路列表:

/sbin/ip link | grep 'UP'

我不明白你的问题的其余部分。

相关内容