更新 port_forward/port_forward.sh
This commit is contained in:
parent
b435b1983a
commit
39b8782f57
@ -1,5 +1,27 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Function to check and enable IP forwarding
|
||||||
|
check_and_enable_ip_forwarding() {
|
||||||
|
local protocol=$1
|
||||||
|
if [ "$protocol" == "ipv4" ]; then
|
||||||
|
local ip_forwarding=$(sysctl -n net.ipv4.ip_forward)
|
||||||
|
if [ "$ip_forwarding" -ne 1 ]; then
|
||||||
|
echo "Enabling IPv4 forwarding..."
|
||||||
|
sysctl -w net.ipv4.ip_forward=1
|
||||||
|
else
|
||||||
|
echo "IPv4 forwarding is already enabled."
|
||||||
|
fi
|
||||||
|
elif [ "$protocol" == "ipv6" ]; then
|
||||||
|
local ip_forwarding=$(sysctl -n net.ipv6.conf.all.forwarding)
|
||||||
|
if [ "$ip_forwarding" -ne 1 ]; then
|
||||||
|
echo "Enabling IPv6 forwarding..."
|
||||||
|
sysctl -w net.ipv6.conf.all.forwarding=1
|
||||||
|
else
|
||||||
|
echo "IPv6 forwarding is already enabled."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Function to add port forwarding rule
|
# Function to add port forwarding rule
|
||||||
add_port_forwarding() {
|
add_port_forwarding() {
|
||||||
local protocol=$1
|
local protocol=$1
|
||||||
@ -7,9 +29,17 @@ add_port_forwarding() {
|
|||||||
local dest_ip=$3
|
local dest_ip=$3
|
||||||
local dest_port=$4
|
local dest_port=$4
|
||||||
|
|
||||||
|
# Check and enable IP forwarding
|
||||||
|
check_and_enable_ip_forwarding $protocol
|
||||||
|
|
||||||
# Add the port forwarding rule
|
# Add the port forwarding rule
|
||||||
|
if [ "$protocol" == "ipv4" ]; then
|
||||||
iptables -t nat -A PREROUTING -p tcp --dport $src_port -j DNAT --to-destination $dest_ip:$dest_port
|
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
|
iptables -t nat -A POSTROUTING -p tcp -d $dest_ip --dport $dest_port -j MASQUERADE
|
||||||
|
elif [ "$protocol" == "ipv6" ]; then
|
||||||
|
ip6tables -t nat -A PREROUTING -p tcp --dport $src_port -j DNAT --to-destination [$dest_ip]:$dest_port
|
||||||
|
ip6tables -t nat -A POSTROUTING -p tcp -d $dest_ip --dport $dest_port -j MASQUERADE
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Port forwarding added: $src_port -> $dest_ip:$dest_port"
|
echo "Port forwarding added: $src_port -> $dest_ip:$dest_port"
|
||||||
}
|
}
|
||||||
@ -22,8 +52,13 @@ delete_port_forwarding() {
|
|||||||
local dest_port=$4
|
local dest_port=$4
|
||||||
|
|
||||||
# Delete the port forwarding rule
|
# Delete the port forwarding rule
|
||||||
|
if [ "$protocol" == "ipv4" ]; then
|
||||||
iptables -t nat -D PREROUTING -p tcp --dport $src_port -j DNAT --to-destination $dest_ip:$dest_port
|
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
|
iptables -t nat -D POSTROUTING -p tcp -d $dest_ip --dport $dest_port -j MASQUERADE
|
||||||
|
elif [ "$protocol" == "ipv6" ]; then
|
||||||
|
ip6tables -t nat -D PREROUTING -p tcp --dport $src_port -j DNAT --to-destination [$dest_ip]:$dest_port
|
||||||
|
ip6tables -t nat -D POSTROUTING -p tcp -d $dest_ip --dport $dest_port -j MASQUERADE
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Port forwarding deleted: $src_port -> $dest_ip:$dest_port"
|
echo "Port forwarding deleted: $src_port -> $dest_ip:$dest_port"
|
||||||
}
|
}
|
||||||
@ -32,17 +67,18 @@ delete_port_forwarding() {
|
|||||||
list_port_forwarding() {
|
list_port_forwarding() {
|
||||||
echo "Current port forwarding rules:"
|
echo "Current port forwarding rules:"
|
||||||
iptables -t nat -L PREROUTING -n -v --line-numbers
|
iptables -t nat -L PREROUTING -n -v --line-numbers
|
||||||
|
ip6tables -t nat -L PREROUTING -n -v --line-numbers
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main script logic
|
# Main script logic
|
||||||
if [ "$1" == "add" ] && [ "$2" == "ipv4" ]; then
|
if [ "$1" == "add" ]; then
|
||||||
add_port_forwarding "tcp" $3 $4 $5
|
add_port_forwarding $2 $3 $4 $5
|
||||||
elif [ "$1" == "delete" ] && [ "$2" == "ipv4" ]; then
|
elif [ "$1" == "delete" ]; then
|
||||||
delete_port_forwarding "tcp" $3 $4 $5
|
delete_port_forwarding $2 $3 $4 $5
|
||||||
elif [ "$1" == "list" ]; then
|
elif [ "$1" == "list" ]; then
|
||||||
list_port_forwarding
|
list_port_forwarding
|
||||||
else
|
else
|
||||||
echo "Usage: $0 add ipv4 <src_port> <dest_ip> <dest_port>"
|
echo "Usage: $0 add <ipv4|ipv6> <src_port> <dest_ip> <dest_port>"
|
||||||
echo " $0 delete ipv4 <src_port> <dest_ip> <dest_port>"
|
echo " $0 delete <ipv4|ipv6> <src_port> <dest_ip> <dest_port>"
|
||||||
echo " $0 list"
|
echo " $0 list"
|
||||||
fi
|
fi
|
Loading…
Reference in New Issue
Block a user