danny

Addnat.sh
#!/usr/bin/env bash echo -e "Please input your server mai...
扫描右侧二维码阅读全文
12
2019/01

Addnat.sh

#!/usr/bin/env bash
echo -e "Please input your server main ip"
        stty erase '^H' && read -p "(such as 8.8.8.8):" main_ip
        [[ -z "${main_ip}" ]] && echo -e "cancel..." && exit 1
echo -e "Please input how many /24 you want to use, max is 5"
        stty erase '^H' && read -p "(such as 1):" user_ip_num
        [[ -z "${user_ip_num}" ]] && echo -e "cancel..." && exit 1
iptables -t nat -F
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to ${main_ip}
user_ip_head="10.0."
for (( c = 1; c <= ${user_ip_num}; c++ ));do
    for (( d = 1; d <= 255; d++ ));do
        user_ip=${user_ip_head}${c}"."${d}
        if (("$d" < 10)); then
            ssh_port="6"${c}"00"${d}
            user_port_first=${c}"00"${d}"0"
            user_port_last=${c}"00"${d}"9"
        elif (("$d" < 100)); then
            ssh_port="6"${c}"0"${d}
            user_port_first=${c}"0"${d}"0"
            user_port_last=${c}"0"${d}"9"
        else
            ssh_port="6"${c}${d}
            user_port_first=${c}${d}"0"
            user_port_last=${c}${d}"9"
        fi
        iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport ${ssh_port} -j DNAT --to-destination ${user_ip}:22
        iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport ${user_port_first}:${user_port_last} -j DNAT --to-destination ${user_ip}
        iptables -t nat -A PREROUTING -i eth0 -p udp -m udp --dport ${user_port_first}:${user_port_last} -j DNAT --to-destination ${user_ip}    
    done
done
service iptables save
service iptables restart
echo -e "It seems done"

每个 IP 分配 10 个应用端口+1 个 ssh 端口

IP 结构

10.0.C.D

端口设置

根据 ip 的 C 和 D 设置,D 不足 10 补两个 0,不足 100 补一个 0

ssh 端口 为 6 C D

例如 10.0.1.2 ssh 端口 61002

10.0.2.25 ssh 端口 62025

10.0.3.154 ssh 端口 63154

可用应用端口为 C D 0 - C D 9

例如 10.0.1.2 应用端口 10020-10029

10.0.2.25 应用端口 20250-20259

10.0.3.154 应用端口 31540-31549


转自@EQblog

Last modification:January 12th, 2019 at 12:35 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment