From owner-freebsd-questions@freebsd.org Thu Oct 1 22:46:04 2015 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 3E1C7A0E3A7 for ; Thu, 1 Oct 2015 22:46:04 +0000 (UTC) (envelope-from freebsd-doc@fjl.co.uk) Received: from bs1.fjl.org.uk (bs1.fjl.org.uk [84.45.41.196]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "bs1.fjl.org.uk", Issuer "bs1.fjl.org.uk" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id D69B018C1 for ; Thu, 1 Oct 2015 22:46:02 +0000 (UTC) (envelope-from freebsd-doc@fjl.co.uk) Received: from [192.168.1.35] (mux.fjl.org.uk [62.3.120.246]) (authenticated bits=0) by bs1.fjl.org.uk (8.14.4/8.14.4) with ESMTP id t91Mjrpb000380 (version=TLSv1/SSLv3 cipher=DHE-DSS-AES128-SHA bits=128 verify=NO) for ; Thu, 1 Oct 2015 23:45:56 +0100 (BST) (envelope-from freebsd-doc@fjl.co.uk) Message-ID: <560DB7A3.1000707@fjl.co.uk> Date: Thu, 01 Oct 2015 23:45:55 +0100 From: Frank Leonhardt User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: freebsd-questions@freebsd.org Subject: Re: FreeBSD and two different networks References: <6371943.nqOrXjNnlq@thinkpad> In-Reply-To: <6371943.nqOrXjNnlq@thinkpad> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.20 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Oct 2015 22:46:04 -0000 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.