Skip to content

Instantly share code, notes, and snippets.

@Asher256
Created December 16, 2019 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Asher256/3b78f0e164b635b72feb34a61b052db0 to your computer and use it in GitHub Desktop.
Save Asher256/3b78f0e164b635b72feb34a61b052db0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env /bin/sh
#
# This script uses Linux iptables to route and masquerade ipv4 traffic
# from a network interface to another.
#
# License: MIT
#
# Author: Asher256
# Github: https://github.com/Asher256
# Website: http://www.asher256.com/
#
set -e
set -u
in_nic=eth0
out_nic=eth1
# Flush old iptables rules
iptables -F; iptables -X
iptables -t nat -F; iptables -t nat -X
iptables -t mangle -F; iptables -t mangle -X
# Iptables policies (accept OUTPUT only)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# Route and masquerade traffic
iptables -A FORWARD -i "$in_nic" -o "$out_nic" -j ACCEPT
iptables -A FORWARD -i "$out_nic" -o "$in_nic" -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t nat -A POSTROUTING -o "$out_nic" -j MASQUERADE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment