Skip to content

Instantly share code, notes, and snippets.

@mrngm
Last active June 23, 2023 09:52
Show Gist options
  • Save mrngm/3179142 to your computer and use it in GitHub Desktop.
Save mrngm/3179142 to your computer and use it in GitHub Desktop.
#!/bin/bash
function print_help() {
echo "Easy 4-step NAT tool"
echo "Usage: $0 internal-dev external-dev ip-range"
echo ""
echo "internal-dev e.g. eth0"
echo "external-dev e.g. wlan0"
echo "ip-range e.g. 192.168.0.1/24"
}
if [[ $BASH_ARGC < 3 ]]; then
print_help
else
echo -n "Enabling ip_forward in /proc/sys/net/"
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "."
echo -n "Setting firewall rules... "
iptables -A FORWARD -i "$2" -o "$1" -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i "$1" -o "$2" -j ACCEPT
iptables -t nat -A POSTROUTING -s "$3" -o "$2" -j MASQUERADE
echo "done."
echo "You have just enabled NAT from $1 to $2 using range $3"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment