From b435b1983a7aabad20c33c1ea3c3942e25d8a761 Mon Sep 17 00:00:00 2001 From: Randall Date: Mon, 18 Nov 2024 16:30:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20port=5Fforward/port=5Fforw?= =?UTF-8?q?ard.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port_forward/port_forward.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/port_forward/port_forward.sh b/port_forward/port_forward.sh index 8f9a4c5..af766c6 100644 --- a/port_forward/port_forward.sh +++ b/port_forward/port_forward.sh @@ -14,6 +14,20 @@ add_port_forwarding() { echo "Port forwarding added: $src_port -> $dest_ip:$dest_port" } +# Function to delete port forwarding rule +delete_port_forwarding() { + local protocol=$1 + local src_port=$2 + local dest_ip=$3 + local dest_port=$4 + + # Delete the port forwarding rule + iptables -t nat -D PREROUTING -p tcp --dport $src_port -j DNAT --to-destination $dest_ip:$dest_port + iptables -t nat -D POSTROUTING -p tcp -d $dest_ip --dport $dest_port -j MASQUERADE + + echo "Port forwarding deleted: $src_port -> $dest_ip:$dest_port" +} + # Function to list port forwarding rules list_port_forwarding() { echo "Current port forwarding rules:" @@ -23,9 +37,12 @@ list_port_forwarding() { # Main script logic if [ "$1" == "add" ] && [ "$2" == "ipv4" ]; then add_port_forwarding "tcp" $3 $4 $5 +elif [ "$1" == "delete" ] && [ "$2" == "ipv4" ]; then + delete_port_forwarding "tcp" $3 $4 $5 elif [ "$1" == "list" ]; then list_port_forwarding else echo "Usage: $0 add ipv4 " + echo " $0 delete ipv4 " echo " $0 list" fi \ No newline at end of file