Thursday, September 16, 2010

Connecting to Multiple Networks

Here in my job I have to connect to two networks, one the intranet of the company and other a 3G network in order to access my email (since I am not a employe I don't have a company mail, and use a free one for everything).

Since I only really need to use the company network to access the nodes I do support, the best option would be connect to both network and use the company one when I try to access the nodes, and the 3G network for everything else (which would basically mean all the internet). Before using this solution I was always needing to change from one network to another depending on what I needed to do.

So, how to do that? It is so incredible easy that took me some weeks to find out! ;)

Basically you first need to connect to both networks (one at a time), and take a look at your routing table.

Ok, so, first I connect to my company network (via cable, which in my case mean that interface eth0 will be used), and it gives me a routing table like the following (obviously, I am changing the address):
$ netstat -rn 
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.10.10.1   0.0.0.0         UG        0 0          0 eth0

Ok, so lets disconnect and see how it looks like when connecting to the 3G (which in my case is the ppp0 interface):

$ netstat -rn 
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1   0.0.0.0         UG        0 0          0 ppp0

It can have some other lines in the configuration, but the "0.0.0.0" line (depending on the configuration it will be shown as "default").
Well, once you have the default route information, just connect to both networks, delete the default route, and add the route you want, in my case I do something like this:

route del default
route add -net 10.10.8.0/23 gw 10.10.10.1 dev eth0
route add -net 10.11.0.0/16 gw 10.10.10.1 dev eth0
route add -net 10.12.50.0/24 gw 10.10.10.1 dev eth0
route add default gw 192.168.1.1 dev ppp0

Where the first line delete the current default route, the 3 next lines add the routing to the nodes I need to have access in my work, and the other creates a default route for everything else to go through the 3G connection.

Hope it helps you people!

No comments: