Some-Shell/port_forward/port_forward.sh

48 lines
1.5 KiB
Bash
Raw Normal View History

2024-11-18 16:23:47 +08:00
#!/bin/bash
# Function to add port forwarding rule
add_port_forwarding() {
local protocol=$1
local src_port=$2
local dest_ip=$3
local dest_port=$4
# Add the port forwarding rule
iptables -t nat -A PREROUTING -p tcp --dport $src_port -j DNAT --to-destination $dest_ip:$dest_port
iptables -t nat -A POSTROUTING -p tcp -d $dest_ip --dport $dest_port -j MASQUERADE
echo "Port forwarding added: $src_port -> $dest_ip:$dest_port"
}
2024-11-18 16:30:03 +08:00
# 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"
}
2024-11-18 16:23:47 +08:00
# Function to list port forwarding rules
list_port_forwarding() {
echo "Current port forwarding rules:"
iptables -t nat -L PREROUTING -n -v --line-numbers
}
# Main script logic
if [ "$1" == "add" ] && [ "$2" == "ipv4" ]; then
add_port_forwarding "tcp" $3 $4 $5
2024-11-18 16:30:03 +08:00
elif [ "$1" == "delete" ] && [ "$2" == "ipv4" ]; then
delete_port_forwarding "tcp" $3 $4 $5
2024-11-18 16:23:47 +08:00
elif [ "$1" == "list" ]; then
list_port_forwarding
else
echo "Usage: $0 add ipv4 <src_port> <dest_ip> <dest_port>"
2024-11-18 16:30:03 +08:00
echo " $0 delete ipv4 <src_port> <dest_ip> <dest_port>"
2024-11-18 16:23:47 +08:00
echo " $0 list"
fi