BIND, dynamic DNS, failover A records

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(minor updates)
m (Reverted edits by DavidYoung (talk) to last revision by
Line 111: Line 111:
Perfect!  Now, the BSDcompany folks force an apparent fail condition on WAN2 to make sure it  ... \n
Perfect!  Now, the BSDcompany folks force an apparent fail condition on WAN2 to make sure it  ... \n
== A Brief History of Timekeeping ==
"For millennia, humans have sought to make sense of time, to visualize it, to ride its arrow, to hack it, to understand biological connection to it. Time is the very foundation of conscious experience, writes Dan Falk in In Search of Time: The History, Physics, and Philosophy of Time. And yet that awareness has a long history of friction -- to mark and measure the passage of time has proven remarkably challenging.
[[ A Brief History of Timekeeping]]
[[ - good, positive news, inspirational stories, articles]]
== Journey to the End of the Earth ==
I realized quickly, after just having traveled to various villages in rural India, that distance is relative. Hailing from a city like San Francisco, going even a few hours outside of town is far but twelve hours outside of a major city? I half expected to run into another country.
[[ Journey to the End of the Earth]]
[[ - good, positive news, inspirational stories, articles]]
== Why You Should Always Trust Yourself ==
Trust yourself. You know more than you think you do. Benjamin SpockAs time passes by and the more work you will do on discovering and improving yourself, the more you will realize that the ancient Latin quotation: Ne te quaesiveris extra - Do not look outside of yourself for the truth, is true.
[[ Why You Should Always Trust Yourself]]
[[ - good, positive news, inspirational stories, articles]]
== Guerilla Gardener Plants Joy in Potholes ==
Theyre the bane of cyclists and motorists alike, but one urban gardener has grown a fondness for potholes after deciding to spruce up cities around Europe by filling them up with miniature flower arrangements. Australian Steve Wheen, 34, who lives in London, has been using flowers and small-scale objects to transform urban potholes for the last three years. The self-styled guerrilla gardener has created mini gardens all around his home city but has now decided to bring joy to commuters across Europe with his unusual pothole creations.
[[ Guerilla Gardener Plants Joy in Potholes]]
[[ - good, positive news, inspirational stories, articles]]
== A Quick Guide To The Art of Listening ==
The most basic of all human needs is the need to understand and be understood. The best way to understand people is to listen to them. Ralph Nichols
[[ A Quick Guide To The Art of Listening]]
[[ - good, positive news, inspirational stories, articles]]

Revision as of 17:25, 25 August 2012


The problem: inexpensive but unreliable ISPs

If you've got a multi-homed network with multiple IP addresses from different ISPs, but you aren't a big enough organization to convince your ISPs to build BGP routes to connect to each other at your network, you will probably find it really handy to have a single DNS record that will automatically choose the best way to get to your network from the outside world.

In this example, "BSDcompany" runs a small office network ( and a server in a colocated network facility ( Frequently, they need to access network resources inside the office from the internet. Since neither of the two ISPs available at BSDcompany's office are particularly reliable, BSDcompany has a cable modem from one of them, a DSL modem from the other, and a dual-WAN router. Both the cable and the DSL use dynamic IP addresses, and the company already has a server in the office doing dynamic DNS updates to and

BSDcompany's dual-WAN router provides load balancing and automatic failover redundancy for internet access from within the office. But BSDcompany wants similar redundancy and balancing from the outside coming in as well. So instead of randomly trying and to see which (if either) is working at any particular time, they just want to be able to use a single name all the time and have it automatically take them to whichever ISP is up and/or faster at the moment.

The solution: (another original)

BSDcompany decides to set up a cron job on their colo server to check the status and latency of each of their office WAN IPs. That script will then automatically update a third A record,, with whichever is currently the quicker of the two office WANs to respond - and if both WANs are down, it will delete the record entirely until one or the other of them comes back up.

(Like the script in the previous dynamic DNS article, the variables in UPPERCASE are things you should set to match your own situation, while the ones in lower or mixed case are generally things you shouldn't need to mess with.)


# Copyright (c) 05-20-2006, JRS System Solutions
# All rights reserved under standard BSD license
# details:
# Check each of two public IPs for the same multi-homed host,
# and set a dynamic DNS A record to point to the lower latency
# of the two.  If both routes are down, delete the hostname
# entirely until one or both IPs come back up.

$WANDNS1 = '';
$WANDNS2 = '';
$KEYFILE = '';
$KEYDIR = '/usr/home/ddns';
$TTL = '10';

@wan1 = split(/\n/,`/sbin/ping -qc 1 -t 1 $WANDNS1`);
@wan2 = split(/\n/,`/sbin/ping -qc 1 -t 1 $WANDNS2`);

$wan1[0] =~ /\((\d*?\.\d*?\.\d*?\.\d*?)\)/;
$wan1_ip = $1;
if ($wan1_ip == '') { $wan1_ip = 'NO HOST FOUND'; }
$wan2[0] =~ /\((\d*?\.\d*?\.\d*?\.\d*?)\)/;
$wan2_ip = $1;
if ($wan2_ip == '') { $wan2_ip = 'NO HOST FOUND'; }

$wan1[3] =~ /(\d*?) packets received/;
$wan1_rcvd = $1;
$wan2[3] =~ /(\d*?) packets received/;
$wan2_rcvd = $1;

$wan1[4] =~ /\/(\d*?\.\d*?)\//;
$wan1_time = $1;
$wan2[4] =~ /\/(\d*?\.\d*?)\//;
$wan2_time = $1;

if ($wan1_rcvd != 1 && $wan2_rcvd == 1) {
        print "WAN1 [$wan1_ip]: NO RESPONSE\nWAN2 [$wan2_ip]: $wan2_time" . "ms\nSET $HOSTNAME: WAN2\n";
} elsif ($wan1_rcvd == 1 && $wan2_rcvd != 1) {
        print "WAN1 [$wan1_ip]: $wan1_time" . "ms\nWAN2 [$wan2_ip]: NO RESPONSE\nSET $HOSTNAME: WAN1\n";
} elsif ($wan1_rcvd != 1 && $wan2_rcvd !=1) {
        print "WAN1 [$wan1_ip]: NO RESPONSE\nWAN2 [$wan2_ip]: NO RESPONSE\nDELETE $HOSTNAME\n";
} elsif ($wan1_time <= $wan2_time) {
        print "WAN1 [$wan1_ip]: $wan1_time" . "ms\nWAN2 [$wan2_ip]: $wan2_time" . "ms\nSET $HOSTNAME: WAN1\n";
} else {
        print "WAN1 [$wan1_ip]: $wan1_time" . "ms\nWAN2 [$wan2_ip]: $wan2_time" . "ms\nSET $HOSTNAME: WAN2\n";

chdir ($KEYDIR);
open (NSUPDATE, "| /usr/sbin/nsupdate -k $KEYFILE");
print NSUPDATE "server $NAMESERVER\n";
print NSUPDATE "update delete $HOSTNAME A\n";
if ($dnsip ne 'NO') {
        print NSUPDATE "update add $HOSTNAME $TTL A $dnsip\n";
# print NSUPDATE "show\n";
print NSUPDATE "send\n";
close (NSUPDATE);

Setting up permissions

To minimize security risks, the gurus at BSDcompany create a new user named "ddns", put this script and the copies of the key files for the zone (which they already had, when they set up their dynamic DNS earlier) in the "ddns" user's home directory, and make sure to set the permissions on everything as restrictively as possible before setting up the cron job to actually run it.

coloserver# pw useradd ddns -s /sbin/nologin -d /usr/home/ddns
coloserver# mkdir /home/ddns
coloserver# cp /etc/namedb/zones/keys/ .
coloserver# cp /etc/namedb/zones/keys/ .
coloserver# chmod 400*
coloserver# chmod 500
coloserver# ls -l
-r--------  1 ddns  wheel   130 May 20 12:22
-r--------  1 ddns  wheel   145 May 20 13:17
-r-x------  1 ddns  wheel  3108 May 23 01:27
coloserver# 'su ddns
This account is currently not available.

Excellent: the ddns account is present but cannot be interactively logged into, the key files are readable (but not writeable or executable) only to it, and the script is executable (but not writeable) only to it. Now that the permissions are correct, it's time to do a test run - we'll run the script manually (using sudo to do so as the user ddns, just like the cron job will) before we set it up to run automatically.

Testing the script manually

coloserver# sudo -u ddns /usr/bin/perl /usr/home/ddns/
WAN1 []: 94.302ms
WAN2 []: 85.341ms
coloserver# ping -qc 1
PING ( 56 data bytes

--- ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max/stddev = 85.038/85.038/85.038/0.000 ms

Perfect! Now, the BSDcompany folks force an apparent fail condition on WAN2 to make sure it ... \n

Personal tools