I'm on a mac and trying to route a particular address though a specific gateway on my wifi connection.
I'm using:
route add -host 54.81.143.201 192.168.15.1Sometimes this will work, other times it wont. What I found is that the interface it chooses is different every time. It needs ot be en0 to work
netstat -nr output when it doesn't work:
54.81.143.201 192.168.15.1 UGHS 1 89 en5This is when it does work: (note en0)
54.81.143.201 192.168.15.1 UGHS 0 1 enWhy am I doing this? Because our company has a proxy that HipChat doesn't work on. So I'm routing hipchat traffic through an open wifi network while still being on my works ethernet.
EDIT:
I also tried adding the entry using just the interface
route add -host 54.81.143.201 -interface en0
54.81.143.201 78:31:c1:c7:52:74 UHS 0 2 en0HipChat fails to connect.
EDIT 2: Someone asked for my whole routing table, here it is today. Note that 54.81.143.201 is now bound to en3 and not en0
Internet:
Destination Gateway Flags Refs Use Netif Expire
default 10.7.90.1 UGSc 31 6 en3
10.7.90/24 link#4 UCS 4 0 en3
10.7.90.1 0:23:ac:3d:db:c2 UHLWIir 16 0 en3 1200
10.7.90.44 40:6c:8f:19:4a:bb UHLWI 0 3 en3 946
10.7.90.63 127.0.0.1 UHS 0 0 lo0
54.81.143.201 192.168.15.1 UGHS 0 0 en3
127 127.0.0.1 UCS 0 0 lo0
127.0.0.1 127.0.0.1 UH 3 209 lo0
169.254 link#4 UCS 1 0 en3
169.254.255.255 0:23:ac:3d:db:c2 UHLSW 0 0 en3 5 9 Answers
Try:
route add -host 54.81.143.201 -interface en0 7 As others indicated, this is actually 3 problems.
Your wireless interface seems to be changing between en0, en3, and en5.
On my MacBook Air, en0 is always wireless; Thunderbolt-to-Ethernet is always en3, and USB-to-Ethernet is always en5. But if you plug in an adapter to a different port on your Mac, then its NIC name changes. You need to solve this first. Make sure your wireless always has the same name. Otherwise when you enter the static route command, if there is no NIC plugged in to the
en0location, the command will (obviously) fail with an "address error" (physical address has no link).Likewise, make sure the adapter is always connecting to the same SSID. The gateway address obviously has to be valid for the subnet, and different WiFi networks will have different subnets. This can cause another type of error.
You didn't specify if wireless is your only network connection. Given the above, I guess not...? This and virtual networking due to VMware or Parallels can cause additional complexities. (For example, if both connected networks use the same IP space... Virtual machines often are bridged and have their own IPs/routes/links...) Posting network topology would help.
Once that's done, try either
sudo route add -host 54.81.143.201 -iface en0orsudo ipfw. If you're not sure of the adapter's name, you can specify its MAC address instead, like this:sudo route add -host 54.81.143.201 -link 14:10:9f:e7:fd:0aRelated:
If you reboot, this may not persist. You'll need to handle that separately.
sudo route -n add -net 54.81.143.201/32 192.168.15.1 0 I was able to add a route across an interface by using the -link option to specify a MAC address.
route add -host 54.81.143.201 -link [mac addr of 192.168.15.1 on en0]
That will send traffic for 54.81.143.201 out the appropriate interface.
You do have two separate 192.168.15.* host addresses assigned, one to each interface, right? Else, you may send traffic out of either interface, but traffic will return on whichever source IP the packets have.
This solution works on latest MacOS 10.12 (Sierra). Here's the Gist.
#!/bin/bash
# NOTE: wifi network interface is: en1
wifi_router=192.168.200.1
wifi_address=en1:ec.35.86.4f.00.cc
TOADDR=`ifconfig en1 inet | sed -nl 's/\w*inet \([^ ]*\).*/\1/p'`
TO=`echo -n ${TOADDR//[[:space:]]}`
echo "ADDING ROUTE TO $1 VIA en1 (wi-fi): $TO"
route -n add -host $1 $wifi_router -ifp $wifi_address -ifa $TO -static
echo ""
echo "ROUTE ADDED:"
route get $1Use like this:
> sudo ./route_wifi.sh IP_ADDRESSIt assumes that wifi interface is: en1.
Don't forget to put correct values for wifi_router and wifi_address variables. Note wifi_address format, which is: network interface name':'interface mac address with '.' delimiters. Sure most of required information can be parsed out of ifconfig command output, but I'm just too lazy for that =)
1The OS X route command is documented here. The -ifscope parameter and its value allow you to specify an interface-bound route.
This is, however, not what you want. You need to fix your networks so their IP ranges are unique. Other than that, interface metrics (aka priorities) affect which interface is chosen from otherwise equally opportune option.
Here's how to translate the user-defined name 'Wi-Fi' into whatever device name (e.g. en0, en1, en9, ...) that MacOS has assigned at that time.
You can put these functions in a specific script, or just keep them in your .bash_profile.
function get_srvc_name ()
{ cat <<EOF | scutil | \ grep 'UserDefinedName' | \ awk -F': ' '{print $2}'
show Setup:/Network/Service/$1
EOF
}
function get_srvc_ids ()
{ cat <<EOF | scutil | \ sed -nEe '
/ServiceOrder/ { :ids n /[0-9]+ :/ { s/ *[0-9]+ : ([0-9A-Z-]+) */\1/p b ids }
}'
show Setup:/Network/Global/IPv4
EOF
}
function get_srvc_id_by_name ()
{ local srvc_ids=$(get_srvc_ids) for srvc_id in $srvc_ids do local srvc_name=$(get_srvc_name "$srvc_id") if [[ "$srvc_name" == "$1" ]] then echo $srvc_id return fi done
}
function get_int_name ()
{ local srvc_id=$(get_srvc_id_by_name "$1") cat <<EOF | scutil | \ sed -nEe ' s/ *DeviceName : ([a-zA-Z0-9]+) */\1/p'
show Setup:/Network/Service/$srvc_id/Interface
EOF
}Then just call get_int_name 'Wi-Fi' to get the assigned device name.
For example:
route add -host 54.81.143.201 -interface $(get_int_name 'Wi-Fi') So the vendor server you're trying to talk to regarding the service "HipChat" you claim is 54.81.143.201? In this case, I'd make a routing entry for 54.81.143.0 255.255.255.0 to give it a bigger range. Maybe when using the software, you aren't always talking to this specific server, but a cluster of them on the same subnet 54.81.143.0/24. Also, additionally, make sure your route metrics are correct when creating a new entry. If you create a route to 54.81.143.0/24 192.168.15.1 Metric 20 En5, but also have a route to 0.0.0.0/0 10.7.90.1 Metric 10 En0. The computer will ignore your new entry and continue routing traffic through the default route (via En0) because its more preferable. I just skimmered through this and wanted to point that out. Cheers!
You should try adding the NIC name:
route add -net 10.13.0.0 netmask 255.255.0.0 dev NicNameHereThis works for me in CentOS.
1