48

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.1

Sometimes 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     en5

This is when it does work: (note en0)

54.81.143.201      192.168.15.1       UGHS            0        1     en

Why 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     en0

HipChat 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
  • Post the full routing table for the case where it doesn't work. Is 192.168.15.1 reachable through en5? If so, why doesn't it work? If not, it's hard to see how that route got added. – David Schwartz May 19 '14 at 19:56
  • @DavidSchwartz, that's what is interesting to me. 192.168.15.1 is not reachable through en5. The thing works when it correctly gets assigned to en0. – Sean256 May 19 '14 at 21:48
  • Show us the routing table or the configuration of en5. There must be some reason the route is getting installed that way, and that's probably the real problem. – David Schwartz May 19 '14 at 22:39
  • @DavidSchwartz It's not always en5, sometimes it's en3. I'm posting my whole routing table now. – Sean256 May 20 '14 at 16:05
  • In the routing table you showed, en3 is clearly correct. 192.168.15.1 isn't reachable through any other interface. The only way to reach 192.168.15.1 in that routing table is through the default route, right? (Bluntly, it sounds like you have no idea what you are doing. You seem to be surprised that the system is not doing the impossible.) – David Schwartz May 20 '14 at 16:58
43

Try:

route add -host 54.81.143.201 -interface en0
| improve this answer | |
  • 3
    This is what I get when I try that: route: bad address: en0 – Sean256 May 19 '14 at 17:42
  • Sorry I don't have a Mac to test this. It looks like the syntax was wrong. I modified the answer. Please try again. – drkblog May 19 '14 at 17:47
  • 2
    same thing sadly -> route: bad address: en0 – Sean256 May 19 '14 at 19:18
  • Try again. This time I removed the gateway. Also, could you add the ifconfig output to your question to see IP configuration involved? – drkblog May 19 '14 at 19:36
  • I just tried that (Actually I have before too) and it does add an entry to my table, it doesn't solve the issue. HipChat fails to connect. Here is the entry in the routing table when doing it that way ->54.81.143.201 78:31:c1:c7:52:74 UHS 0 2 en0 – Sean256 May 19 '14 at 21:53
4

As others indicated, this is actually 3 problems.

  1. 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 en0 location, 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.

  2. Once that's done, try either sudo route add -host 54.81.143.201 -iface en0 or sudo 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:0a

    Related: https://discussions.apple.com/thread/5049994?searchText=policy%20route

  3. If you reboot, this may not persist. You'll need to handle that separately.

| improve this answer | |
3
sudo route -n add -net 54.81.143.201/32 192.168.15.1
| improve this answer | |
2

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.

| improve this answer | |
  • So I tried what you suggested by using the mac addr, and oddly my routing table shows an entry for en3 and not en0 ->54.81.143.201 00:1d.88.4a.21.da UGHS 0 0 en3 – Sean256 May 20 '14 at 16:00
  • When you do an 'arp -an', what entries do you have...? – Nevin Williams May 20 '14 at 19:04
2

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 $1

Use like this:

> sudo ./route_wifi.sh IP_ADDRESS

It 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 =)

| improve this answer | |
  • Welcome to Superuser. Please try to contain most relevant information from the link in your post. Read more about it here. – styrofoam fly Aug 21 '17 at 10:08
1

The 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.

| improve this answer | |
0

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')
| improve this answer | |
-1

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!

| improve this answer | |
-1

You should try adding the NIC name:

route add -net 10.13.0.0 netmask 255.255.0.0 dev NicNameHere

This works for me in CentOS.

| improve this answer | |
  • I get this -> route: bad address: dev – Sean256 May 19 '14 at 17:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.