You'll find a whole chapter on dnsmasq in my book Alternative DNS Servers. The program has evolved a bit since the book was published, and Stefan Rubner found some new features. This is his report.

Unknown Hosts

After having set up your Dnsmasq environment and painstakingly having added your DHCP definitions and (re)started Dnsmasq, you will most likely discover a little problem there: Dnsmasq will only resolve hosts that are in your DHCP definitions and have requested an IP address from Dnsmasq already.

Now one could of course add all these hosts to /etc/hosts manually. Well, if you're in for some additional typing exercise, there you go. For all others I wrote a little Perl script that'll do the job by reading and parsing the dhcp-host definitions in Dnsmasq's config file:

#!/usr/bin/perl -w
#-----------------------------------------------------#
# dnsmasq_gen_hostlist.pl                             #
# Generate a host compatible list of host entries     #
# from Dnsmasq's dhcp-host definitions                #
#-----------------------------------------------------#
# Authors                                             #
# (sru) Stefan Rubner <stefan at whocares.de>         #
#-----------------------------------------------------#
# Revision History                                    #
# 0.1.0 (sru) First implementation                    #
#-----------------------------------------------------#
# Usage                                               #
# dnsmasq_gen_hostlist.pl <infile> > <outfile>        #
#                                                     #
# If <outfile> is omitted the results are printed to  #
# STDOUT.                                             #
#-----------------------------------------------------#

if (! $ARGV[0]) {
  print "\nUsage:\n";
  print "$0 <infile> > <outfile>\n\n";
  print "If <outfile> is omitted the results are printed to STDOUT.\n\n";
  exit 1;
}

$conf = $ARGV[0];

my %data;
while(<>) {
  next if /^\s*($|#)/;

  if ($_ =~ /^(dhcp-host).*[=,]([a-zA-Z0-9\-_]+),([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*),/) { 
    # print "Host found: $1 ($2 -> $3)\n";
    $data{$2} = "$3\t$2";
  } elsif ($_ =~ /^cname=([a-zA-Z0-9\-_]+),([a-zA-Z0-9\-_]+)/) {
    # print "  Cname found: $2 -> $1\n";
    if ($data{$2}) {
      $data{$2} .= " $1";
    } else {
      print "# FIXME FIXME FIXME\n";
      print "# Definition of CNAME $1 for host $2 before host was defined!\n";
      print "# Please fix your configuration file $conf!!\n";
      print "# FIXME FIXME FIXME\n";
    }
  }
}

foreach $records (sort keys %data ) {
    print "$data{$records}\n";
}

Say you have your main Dnsmasq config in /etc/dnsmasq/dnamasq.conf. To make use of the script above, just add a line at the very end (or anyplace else you deem fit):

addn-hosts=/etc/dnsmasq/dnsmasq.hosts-dynamic

To generate the file you just told Dnsmasq to use as additional hosts resource, call

./dnsmasq_gen_hostlist.pl /etc/dnsmasq/dnsmasq.conf > /etc/dnsmasq/dnsmasq.hosts-dynamic

For convenience, just add that to Dnsmasq's startup script ;) (Adapt and modify as needed, of course)

Same IP for multi-connected host

One of the coolest new features available since Dnsmasq 2.46 is the ability to offer the same IP address to clients with different MAC addresses. From the docs:

# Give a host with ethernet address 11:22:33:44:55:66 or              
# 12:34:56:78:90:12 the IP address 192.168.0.60. Dnsmasq will assume  
# that these two ethernet interfaces will never be in use at the same      
# time, and give the IP address to the second, even if it is already       
# in use by the first. Useful for laptops with wired and wireless           
# addresses.                                                                
dhcp-host=11:22:33:44:55:66,12:34:56:78:90:12,192.168.0.60

Comments

blog comments powered by Disqus