Skip to content

Instantly share code, notes, and snippets.

@adde88
Last active July 13, 2017 18:49
Show Gist options
  • Select an option

  • Save adde88/a52f08ff2ed380d44e07cea38cfcf410 to your computer and use it in GitHub Desktop.

Select an option

Save adde88/a52f08ff2ed380d44e07cea38cfcf410 to your computer and use it in GitHub Desktop.
Iptables script. Port translation. OpenWRT. Gateway
#!/bin/sh
#
# IPTABLES - HELPER SCRIPT
# MADE BY: ZYLLA - [email protected]
#
# I made this for my 4G router, with OpenWRT.
# But it may aswell work on a normal Linux-box with two NIC's acting as a gateway.
#
IPORT="$1" # This is the port on the internet-side, which the outside clients will connect to.
DPORT="$2" # This is the port on the LAN side of your network, iptables will translate IPORT to this port.
DIP="$3" # The IP-address of the machine on your LAN, which are running the service you want to make accessible to the internet.
helper() {
echo -e "$(basename "$0") [-h] [-input-port -destination-port -ip] -- Helper script to setup port forwarding on D-link DWR-956 4G router.
example:
$(basename "$0") 8080 80 10.0.0.2
This will open port 8080 on WAN, and translate it to port 80 on a specified LAN IP."
}
if [ "$1" == "-h" ]; then
helper
exit 0
fi
if [ -z "$1" ] && [ -z "$2" ]; then
helper
exit 0
fi
iptables -t nat -I PREROUTING -p tcp -i usb0 --dport "$IPORT" -j DNAT --to-destination "$DIP":"$DPORT"
iptables -A FORWARD -i usb0 -o br0 -p tcp --dport "$DPORT" -j ACCEPT
# logging below
#iptables -A INPUT -p tcp --dport "$IPORT" -m state --state NEW -j LOG --log-level 4
echo "Success!"
echo "Port "$1" on WAN, should now be translated to port "$2" on your LAN."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment