[deepin exploration] AI写的脚本小工具(端口转发)
Tofloor
poster avatar
neko
deepin
Ecological co-builder
2024-10-10 20:35
Author

本文用于记录AI写的脚本工具。


源码:

#!/bin/bash

# 设置iptables命令
IPTABLES="/sbin/iptables"

# 功能函数
show_rules() {
    echo "当前的转发规则:"
    local rules=$($IPTABLES -t nat -L PREROUTING -n --line-numbers | grep DNAT)
  
    if [[ -z "$rules" ]]; then
        echo "没有规则。"
    else
        echo "$rules" | while read -r line; do
            local rule_number=$(echo "$line" | awk '{print $1}')
            local dport=$(echo "$line" | grep -oP 'dpt:\K[\d]+')
            local dest=$(echo "$line" | grep -oP 'to:\K[0-9.]+:\d+')
            echo "$rule_number: $dport -> $dest"
        done
    fi
}

add_rule() {
    local src_port=$1
    local dest_ip=$2
    local dest_port=$3

    # 检查规则是否已经存在
    if $IPTABLES -t nat -C PREROUTING -p tcp --dport "$src_port" -j DNAT --to-destination "$dest_ip:$dest_port" >/dev/null 2>&1; then
        echo "规则已存在:$src_port -> $dest_ip:$dest_port"
        return
    fi

    # 添加PREROUTING规则
    $IPTABLES -t nat -A PREROUTING -p tcp --dport "$src_port" -j DNAT --to-destination "$dest_ip:$dest_port"
    # 添加POSTROUTING规则
    $IPTABLES -t nat -A POSTROUTING -p tcp -d "$dest_ip" --dport "$dest_port" -j MASQUERADE

    echo "已添加规则:$src_port -> $dest_ip:$dest_port"
}

delete_rule() {
    local rule_number=$1

    # 提取规则信息
    local rule_line=$($IPTABLES -t nat -L PREROUTING -n --line-numbers | grep DNAT | awk -v num="$rule_number" '$1 == num')

    if [[ -z "$rule_line" ]]; then
        echo "规则不存在!"
        return
    fi

    # 提取源端口和目标信息
    local src_port=$(echo "$rule_line" | grep -oP 'dpt:\K[\d]+')
    local dest_ip=$(echo "$rule_line" | grep -oP 'to:\K[0-9.]+')
    local dest_port=$(echo "$rule_line" | grep -oP ':\d+$' | cut -d':' -f2)

    # 删除PREROUTING规则
    $IPTABLES -t nat -D PREROUTING -p tcp --dport "$src_port" -j DNAT --to-destination "$dest_ip:$dest_port"
    # 删除POSTROUTING规则
    $IPTABLES -t nat -D POSTROUTING -p tcp -d "$dest_ip" --dport "$dest_port" -j MASQUERADE

    echo "已删除规则:$src_port -> $dest_ip:$dest_port"
}

# 主程序
case $1 in
    add)
        if [ "$#" -ne 4 ]; then
            echo "用法: $0 add 源端口 目标地址 目标端口"
            exit 1
        fi
        add_rule "$2" "$3" "$4"
        ;;
    del)
        if [ "$#" -ne 2 ]; then
            echo "用法: $0 del 规则编号"
            exit 1
        fi
        delete_rule "$2"
        ;;
    show)
        show_rules
        ;;
    *)
        echo "用法: $0 {add|del|show} [参数...]"
        exit 1
        ;;
esac

效果:

root@SP01:~# bash pf2.sh add
用法: pf2.sh add 源端口 目标地址 目标端口
root@SP01:~# bash pf2.sh add 40011 10.10.10.4 22
已添加规则:40011 -> 10.10.10.4:22
root@SP01:~# bash pf2.sh show
当前的转发规则:
1: 40022 -> 10.10.10.4:220
2: 40011 -> 10.10.10.4:22
root@SP01:~# bash pf2.sh del
用法: pf2.sh del 规则编号
root@SP01:~# bash pf2.sh del 1
已删除规则:40022 -> 10.10.10.4:220
root@SP01:~# bash pf2.sh show
当前的转发规则:
1: 40011 -> 10.10.10.4:22
root@SP01:~# 
Reply Favorite View the author
All Replies
神末shenmo
deepin
Spark-App
2024-10-10 21:08
#1

可以搓一个socat版的,这样就可以在安卓手机用了proud

Reply View the author
neko
deepin
Ecological co-builder
2024-10-10 21:21
#2
神末shenmo

可以搓一个socat版的,这样就可以在安卓手机用了proud

手机好像也可以直接adb端口转发

Reply View the author
Oli
deepin
2024-10-11 01:19
#3

like

Reply View the author
remyxo
deepin
2024-10-18 08:40
#4

学习学习。yeah

Reply View the author