Trying to figure out route add to mask an IP to a router/gateway in Bash
Solution 1
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 192.168.100.0 netmask 255.255.255.0 gw $RouterIP
Adding additional routes.
Solution 2
For eth0
:
sudo ifconfig en0 192.168.100.0 netmask 255.255.255.0
The route add default 192.168.100.1
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.
Related videos on Youtube
Pramod kumar
A Canadian guy that loves to code, and learn more.
Updated on September 18, 2022Comments
-
Pramod kumar over 1 year
Not even sure if I worded this correctly, but here’s what I’m trying to do roughly.
Take an IP such as:
192.168.100.0
and mask it to255.255.255.0
, 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
255.255.255.0
, and all to the same router IP.For example:
192.168.100.0 mask 255.255.255.0 router/gateway 192.168.160.1 192.168.120.0 mask 255.255.255.0 router/gateway 192.168.160.1
Etc…
-
Grady Player almost 13 yearsI re-tagged you should get some more eyes now. Your example above has a gateway that isn't in the network, e.g. 192.168.160.1 is not in 192.168.120.0/24
-
Admin almost 13 yearsThank 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.
-
Grady Player almost 13 yearsthe gateway is how you get out of the local network, eg an address that isn't in 192.168.160.0 if your bitmask is 255.255.255.0, 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 192.168.161.56 will be sent to your gateway address to route properly.
-
Pramod kumar almost 13 yearsYes, 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."
-
Pramod kumar almost 13 yearsBeen refreshing my subnet knowledge: en.wikipedia.org/wiki/Subnetwork. Wouldnt the 255.255.255.0 mask as i stated be the proper subnet mask for the example 192.168.100.0 network prefix? Sorry I just don't follow you guys exactly. I'm definitely no sysadmin.
-
Mucker almost 13 yearsThe problem is that your network of 192.168.100.0 with mask 255.255.255.0 tells the PC that anything in the range of 192.168.100.0-192.168.100.255 is the local LAN address. A gateway as someone has already mentioned is a device on the LAN (usually a router) that is your gateway to other networks but you have listed it as 192.168.160.1. But by this IP is OUTSIDE the range of your own LAN so it won't work. Take a look here for an extremely well structured description on how routing works. I found it the other day think-like-a-computer.com/2011/07/18/how-routing-works
-
-
Cancos almost 13 yearsYou probably meant
route add default 192.168.100.1
-
bbaja42 almost 13 yearsI 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?
-
Grady Player almost 13 yearsyeah you are right you are going to need a gateway inside of the /24 network... fixing.
-
Admin almost 13 yearsGave that a shot, here's the result output: route: writing to routing socket: File exists add net default: gateway 192.168.160.1: File exists
-
Grady Player almost 13 yearsthen does it work, can you ping 192.168.160.1?
-
Admin almost 13 yearsyep :). Here are the commands i used in full sudo ifconfig en0 192.168.100.0 netmask 255.255.255.0 sudo route add default 192.168.160.1. Just to double check, there's about 7 or 8 ip addresses i need to mask in the same fashion, and have them. set to the same gateway. The purpose of this script is to solve an issue we have when connecting a mac computer to a network with more than one gateway. It's an issue at one of our remote offices.
-
Grady Player almost 13 yearsread
man route
figure out how to add alternate routes... you may have to add alias interfaces to have an IP in the network. -
Admin almost 13 yearsWill do. I'll re-word my question so it's a little more clear.