#!/bin/bash

# 配置变量
declare -A ACCOUNTS=(
    ["101"]="091246017239"
    ["102"]="091246176158"
    ["103"]="091246176253"
    ["107"]="091246190492"
    ["108"]="091246190881"
    ["110"]="091246191005"
)
VLANS=("101" "102" "103" "107" "108" "110")
PASSWORD="123456"
INTERFACE="ens160"
CONNECTIONS_PER_VLAN=250


# 分配每个 VLAN 的 TABLE_ID
declare -A VLAN_TABLE_IDS=(
    ["101"]=10
    ["102"]=11
    ["103"]=12
    ["107"]=13
    ["108"]=14
    ["110"]=15
)


# 清理现有配置和日志
echo "Cleaning up existing connections and logs..."
killall pppd 2>/dev/null
rm -f /var/log/ppp/ppp_*.log
sleep 2


# 在清理现有配置部分添加
# 清理现有路由规则
echo "Cleaning up existing routing rules..."
ip rule show | grep -v "from all" | while read line; do
    ip rule del `echo $line | sed 's/:.*//'`
done

# 清理路由表文件中的 PPP 条目
sed -i '/^2[0-9][0-9] ppp/d' /etc/iproute2/rt_tables

# 删除现有 macvlan 接口
for vlan in "${VLANS[@]}"; do
    for i in $(seq 1 $CONNECTIONS_PER_VLAN); do
        MACVLAN_INTERFACE="${INTERFACE}_mac${vlan}_${i}"
        if ip link show "$MACVLAN_INTERFACE" &>/dev/null; then
            echo "Removing existing macvlan interface $MACVLAN_INTERFACE..."
            ip link set "$MACVLAN_INTERFACE" down
            ip link delete "$MACVLAN_INTERFACE" type macvlan
        fi
    done
done


sleep 2


# 更新 /etc/iproute2/rt_tables
for vlan in "${VLANS[@]}"; do
    TABLE_ID=${VLAN_TABLE_IDS[$vlan]}
    if ! grep -q "^$TABLE_ID ppp$vlan" /etc/iproute2/rt_tables; then
        echo "$TABLE_ID ppp$vlan" >> /etc/iproute2/rt_tables
    fi
done


# 确保主网卡正常
echo "Setting up main interface..."
ip link set "$INTERFACE" up
ip link set "$INTERFACE" mtu 1500

# 计数器用于PPP接口命名
ppp_counter=0

# 为每个VLAN和连接创建配置
for vlan in "${VLANS[@]}"; do
    for i in $(seq 1 $CONNECTIONS_PER_VLAN); do
        #MACVLAN_INTERFACE="${INTERFACE}_mac${vlan}_${i}"
        MACVLAN_INTERFACE="mv${vlan}${i}"
        
        echo "Setting up macvlan interface $MACVLAN_INTERFACE with VLAN $vlan Connection $i..."

        # 创建 macvlan 接口
        if ! ip link show "$MACVLAN_INTERFACE" &>/dev/null; then
            echo "Adding macvlan interface $MACVLAN_INTERFACE..."
            ip link add link "$INTERFACE" name "$MACVLAN_INTERFACE" type macvlan mode bridge
            NEW_MAC=$(printf '00:16:3e:%02x:%02x:%02x' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))
            ip link set dev "$MACVLAN_INTERFACE" address "$NEW_MAC"
            ip link set "$MACVLAN_INTERFACE" up
            ip link set "$MACVLAN_INTERFACE" mtu 1500
            echo "Assigned MAC address $NEW_MAC to $MACVLAN_INTERFACE"
        else
            echo "macvlan interface $MACVLAN_INTERFACE already exists."
        fi

        # 创建PPP配置文件
        PPP_PEER="/etc/ppp/peers/dsl_${vlan}_${i}"
        cat > "$PPP_PEER" << EOF
plugin rp-pppoe.so $MACVLAN_INTERFACE
noauth
user "${ACCOUNTS[$vlan]}"
password "${PASSWORD}"
persist
maxfail 0
holdoff 5
unit ${ppp_counter}
defaultroute
usepeerdns
mtu 1492
mru 1492
noaccomp
default-asyncmap
nopcomp
noccp
novj
debug
logfile /var/log/ppp/ppp_${vlan}_${i}.log
hide-password
noipdefault
EOF

        # 启动连接并等待建立
        echo "Starting PPPoE connection for VLAN $vlan Connection $i (ppp${ppp_counter})..."
        pppd call "dsl_${vlan}_${i}" &
                
        # 等待连接建立或失败
        WAIT_TIME=0
        # PPP连接建立部分的修改
        while [ $WAIT_TIME -lt 30 ]; do
            if ip link show "ppp${ppp_counter}" &>/dev/null; then
                if ip addr show "ppp${ppp_counter}" | grep -q "inet "; then
                    PPP_IP=$(ip addr show "ppp${ppp_counter}" | grep -oP 'inet \K[\d.]+')
                    # 修改TABLE_ID的计算方式，使用循环方式保持在10-250范围内
                    TABLE_ID=$((10 + (ppp_counter % 240)))
                    
                    # 使用批处理方式添加路由规则
                    {
                        # 如果路由表已存在，先删除旧的
                        sed -i "/^${TABLE_ID} ppp/d" /etc/iproute2/rt_tables
                        echo "$TABLE_ID ppp${ppp_counter}" >> /etc/iproute2/rt_tables
                        
                        # 删除可能存在的旧规则
                        ip rule del from $PPP_IP lookup $TABLE_ID 2>/dev/null
                        
                        # 添加新的规则
                        ip rule add from $PPP_IP lookup $TABLE_ID priority $((1000 + ppp_counter))
                        ip route add default dev "ppp${ppp_counter}" table $TABLE_ID
                        
                        # 确保ppp0始终是主默认路由
                        if [ $ppp_counter -eq 0 ]; then
                            ip route replace default dev "ppp${ppp_counter}"
                        fi
                    } &> /dev/null
                    
                    echo "Added policy routing for ppp${ppp_counter} with IP $PPP_IP (Table: $TABLE_ID)"
                    break
                fi
            fi
            sleep 1
            WAIT_TIME=$((WAIT_TIME + 1))
        done



        if [ $WAIT_TIME -ge 30 ]; then
            echo "Warning: Connection ppp${ppp_counter} failed to establish in time."
            echo "Checking log file:"
            tail -n 20 "/var/log/ppp/ppp_${vlan}_${i}.log"
        fi
        
        # 增加计数器
        ppp_counter=$((ppp_counter + 1))
        
        # 在每个连接之间添加额外延迟
        sleep 0.2
    done
done





帮我看下 ppp240之后的ip 都联不通，

ppp0~239都是正常的

是不是TABLE_ID ，但是我总共会同时拨号得到1500个ip， 但是table id 最大只能252

有没有什么好办法

我在想一定非要设置tale id 嘛

我想要做到能同时拨到1500个ip，然后从外部请求每个ip都能访问到拨号服务器上搭建的服务

然后拨号服务器内部也支持 curl --interface 拨号ip ifconfig.me  能正常返回指定的ip

注意我的服务器默认没有公网ip，只有拨号后才会有ip， 默认使用ppp0作为 出口ip

发我修改后的完整代码 完整代码 完整代码