Skip to content

Instantly share code, notes, and snippets.

@madlymad
Forked from cpthooch/route_wifi.sh
Last active November 3, 2017 12:25
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 madlymad/045faf19e663ec1243f0798e1cbe2004 to your computer and use it in GitHub Desktop.
Save madlymad/045faf19e663ec1243f0798e1cbe2004 to your computer and use it in GitHub Desktop.
Routing all traffic to particular IP address via wifi network interface on MacOS
#!/bin/bash
# NOTE: wifi/ethernet network interface is: en1
net_interface=en1
mac_network_interface=ifconfig $net_interface ether | sed -nl 's/\w*ether \([^ ]*\).*/\1/p' | sed 's/:/./g'
wifi_router=netstat -nr | grep default | grep $net_interface | perl -nle '/(\d+\.\d+\.\d+\.\d+)/ && print $1'
wifi_address=$net_interface:$mac_network_interface
TOADDR=`ifconfig $net_interface inet | sed -nl 's/\w*inet \([^ ]*\).*/\1/p'`
TO=`echo -n ${TOADDR//[[:space:]]}`
echo "ADDING ROUTE TO $1 VIA $net_interface: $TO"
route -n add -host $1 $wifi_router -ifp $wifi_address -ifa $TO -static
echo ""
echo "ROUTE ADDED:"
route get $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment