Skip to content

Instantly share code, notes, and snippets.

@hydra35
Created August 24, 2012 03:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hydra35/3445138 to your computer and use it in GitHub Desktop.
Save hydra35/3445138 to your computer and use it in GitHub Desktop.
create NAT gateway on OSX
#!/bin/bash
# Get the interface name for the gateway
gwdev=`netstat -nr | grep default | awk '{ print $6 }' | head -1`
# If none are found, set the gateway to en1 (generally Wifi on OS X)
if [ -z "$gwdev" ]; then
gwdev=en1
fi
# Create a bridge, add the Ethernet device
ifconfig bridge0 create
ifconfig bridge0 up
ifconfig bridge0 addm en0
# Give it an IP, route bridge0's traffic to bridge0
ifconfig bridge0 172.20.0.1
route add default -interface bridge0 -ifscope bridge0 -cloning
# Enable IP forwarding, add a firewall rule to send all natd traffic to the real gateway
# Start natd with a whole bunch of options
sysctl -w net.inet.ip.forwarding=1
/sbin/ipfw add 100 divert natd ip from any to any via $gwdev
/usr/sbin/natd -interface $gwdev -use_sockets -same_ports -unregistered_only -dynamic -clamp_mss -enable_natportmap -natportmap_interface en0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment