FreeBSD and two different networks

Frank Leonhardt freebsd-doc at fjl.co.uk
Thu Oct 1 22:46:04 UTC 2015


On 30/09/2015 21:58, Maxim V Filimonov wrote:
> Dear All,
>
> I have a FreeBSD machine at work. It's mostly connected to the so-called
> "internal" network that has a static ip: let it be 172.16.1.113/12. The
> default gateway for that network is 172.16.1.1.
> Sometimes, I need to connect this machine to another network that uses DHCP.
> Its network mask differs: it's 192.168.1.0/24. The second network uses DHCP.
>
> The question is: can I set both of the default gateways (skip DHCP: I can
> configure the interface for the "external" network manually) in some way? Can
> I set up some kind of a daemon that checks whether one of my gateways is
> accessible and forwards all of the traffic through that gateway? Thank you
> very much in advance.

Someone will probably know a good answer for this, but a quick and dirty 
way would be to run a simple script on a cron job (or from rc.local if 
you rebooted each time) and just ping something that's going to be 
different on each network. For example, try running ifconfig to set up 
for the first network (assuming at startup) and then ping the gateway on 
that. If it fails (doesn't return 0) then try the other. As the network 
may take a while to settle, just loop around a few times until one works.

If you're thinking in terms of having two default gateways you're going 
the wrong way. It seems logical to be able to have two, but routing 
doesn't work that way.

At this risk of telling you what you may already know well enough, the 
command to set a default gateway from the command line is "route add 
default 172.16.1.1". Use "route del default" first if necessary.

So, for startup, you'd end up with something like (and don't quote me or 
expect this to be free of syntax error etc):

-----------------------
while :
do

ifconfig ...... whatever for your WORK network
sleep 10

if /sbin/ping -q -o -t 5 -c 1 172.16.1.1 >/dev/null
then
     echo "We're connected to the work network!"
     route del default
     route add default 172.16.1.1
     exit
fi

ifconfig ...... whatever for your HOME network
sleep 10

if /sbin/ping -q -o -t 5 -c 1 192.168.1.1 >/dev/null
then
     echo "We're connected to the home network!"
     route del default
     route add default 192.168.1.1
     exit
fi

done
-----------------------
Having this as in infinite loop is, of course, a bad idea but the exit 
is left as an exercise for the reader.





More information about the freebsd-questions mailing list