#!/bin/sh # # masquerade: Script to masquerade an external network connection # (modem or ADSL) upon bringing the interface up. Name this # script masquerade.sh, put it in /etc/network/if-up.d/ and # make it executable (755). # Change INET_IFACE and LAN_IP_RANGE to suit your needs. # You will need to have packet filtering (iptables) enabled # in your kernel and module iptable_nat. # # This script is intended for Debian systems. May not work on # other Linux distributions. # # Author: Ricardo Yanez , Mon May 30, 2011 # # Define external interface INET_IFACE="eth0" # LAN IP range LAN_IP_RANGE="192.168.0.0/24" # Get IP number INET_IP=`ifconfig $INET_IFACE | egrep "inet addr" | cut -d : -f2 | cut -d \ -f1` # please note the extra space in the last cut, right after \ (very important). # Clear all rules in POSTROUTING chain iptables -t nat -F POSTROUTING # Masquerade echo -n "Masquerading interface $INET_IFACE to source $INET_IP... " iptables -t nat -A POSTROUTING -s $LAN_IP_RANGE -o $INET_IFACE \ -j SNAT --to-source $INET_IP echo "done."