Not even sure if I worded this correctly, but here’s what I’m trying to do roughly.

Take an IP such as: and mask it to, and set the gateway of that route to another IP address.

I'm using bash, on Mac OS X.

To add, I.T. at my work has an issue where Mac computers have a problem when connecting to a network that has more than one gateway/router. The idea is to create a script that checks to see what subnet it is connected to. If it is the subnet with more than one gateway, then we need to add additional routing rules to ensure proper communication.

Its a few different IPs that will all mask to, and all to the same router IP.

For example: mask router/gateway mask router/gateway


  • I re-tagged you should get some more eyes now. Your example above has a gateway that isn't in the network, e.g. is not in Jul 12, 2011 at 20:55
  • Thank you, and an interesting point. My knowledge of networking is unfortunately a bit hazy. I haven't done a lot of that stuff since progamming, and I'm more of a web-app developer.
    – agmcleod
    Jul 12, 2011 at 21:01
  • 1
    the gateway is how you get out of the local network, eg an address that isn't in if your bitmask is, your range of valid addresses is 192.168.160.[0..255] but you shouldn't generally consider 0 to be valid unless your network hardware is set up that way. So an address like will be sent to your gateway address to route properly. Jul 12, 2011 at 21:04
  • This is not really to right question to ask. You should not be having any problems with multiple routers on a network if the network was configured properly, nor should you have to add a series of static routes to the host. Also, doesn't this network have a DHCP server?
    – Keith
    Jul 13, 2011 at 2:28
  • Yes, we do have DHCP setup. In terms of the multiple routers, here's a bit of the explanation i got from IT: "The reason for this script is to resolve an issue when connecting the Mac computer to a network with more than one gateway (router). Normally the automatic network configuration sets these settings but when a network has 2 or more gateways additional routing instructions might need to be provided to the computer’s operating system. On Windows based computers these additional routing rules can be applied automatically, but the automatic configuration is not supported on the Mac."
    – agmcleod
    Jul 13, 2011 at 17:39

2 Answers 2


For eth0:

sudo ifconfig en0 netmask

The route add default may need to be adjusted if it isn’t the default route.

EDIT: Fixed the route. Also don’t generally use the zero address for a node in the network unless you know what you are doing.

  • You probably meant route add default
    – KevinDTimm
    Jul 12, 2011 at 19:30
  • I can understand what your answer means, but I'm not sure what is the question :D . Could you reword the question perhaps to make it more clear?
    – bbaja42
    Jul 12, 2011 at 19:31
  • yeah you are right you are going to need a gateway inside of the /24 network... fixing. Jul 12, 2011 at 19:33
  • Gave that a shot, here's the result output: route: writing to routing socket: File exists add net default: gateway File exists
    – agmcleod
    Jul 12, 2011 at 19:35
  • then does it work, can you ping Jul 12, 2011 at 19:39

I spoke to my brother about this, who does networking at his job, sending him the original email. It would seem I explained this rather badly, and I apologize. But essentially, this is what I was looking for:

route add netmask gw $RouterIP

Adding additional routes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .