Next Previous Contents

5. Automatic MAC address detection

We, at SLAC, are using static IP addresses for all our computers. When getting a whole bunch of new machines you need to get their MAC addresses into the dhcpd.conf file.

If you can use dynamic IP addresses, just designate an interval of IP addresses in your dhcpd.conf file to be handed out to your clients.

However, I have written a script to detect MAC address for new machines automatically and fully unattended. It works by exploiting the topological knowledge about which node is connected to which port on the switch. It uses SNMP to query the bridging table of the switch the machines have to be connected to. It writes a new dhcpd.conf file which now additionally contains the newly detected MAC addresses. The script currently works for CISCO Catalyst 6509 and 3750 switches and is available upon request from the author.

Here is the usage message of the script:

Script to query a switch for MAC addresses.
 -- Version 4.2,   Author: Alf Wachsmann <alfw@SLAC.Stanford.EDU> --

Usage: MAC_print.pl 
       Optional Flags:
       -c(onfig) <file>: configuration file. The default config file is
                         expected to be in the same directory as this script.
       -p(xeprepare):    link PXElinux config file in tftpboot directory for
                         each IP address to do netboot.
       -r(reset):        remove all links to PXElinux config file in tftpboot
                         directory for each IP address to do localboot.
       -v(erbose):       be a bit more chatty.
       -t(est):          test the configuration file.
       -d(ebug):         lots of debug output;
                         especially usefull with -test option.
       -h(elp):          print this help.

See text at end of this script for a description of how it works.

A configuration file for the script looks like this:

# System type can be 'Linux' or 'Solaris'.
# The scripts outputs either a dhcpd.conf addition for Linux or
# just MAC and IP address for Solaris
$sys_type = 'Linux';

# Subnet (in fact: VLAN) in which the new machines should be in (see below):
$subnet = 0;

# which switch to query:
$switch = 'swh-farm8';

# Which ports on which cards to look for. The order here
# determines which IP address is assigned to which port!
# (There are at most 48 ports per card.)
#            card => [from port, to port]
%portdef = (
                3 => [1, 48],
                4 => [1, 48],
);

# Range of IP addresses which should be used for this cluster of
# machines. Prefix is always "192.168.".
# Column one is the subnet.
# The number of IP addresses in this range MUST match the number
# of ports to observe! This consistency is checked by the script.
%iprange = (
                0 => [11, 106]
);

If you are using this script you want to use another script which checks for changes in the dhcpd.conf file and re-starts the DHCP server if necessary (the ISC-DHCP server does not understand a HUP signal).

The script which detects the MAC addresses cannot do this because it might run on a different host then the DHCP server (this means the dhcpd.conf file has to be in a shared files system (AFS, NFS, DFS)).

#!/bin/sh
# Author: (c) Alf Wachsmann <alfw@SLAC.Stanford.edu>, July 09, 2001.
# Usage:  Just start the script. CTRL-c out of it.

conffile="/afs/slac/Kickstart/DHCP/etc/dhcpd.conf"

# get the MD5 checksum of the dhcpd.conf file
md5_res=`md5sum $conffile | awk '{print $1}'`

/etc/rc.d/init.d/dhcpd start

while true
do
  md5_new=`md5sum $conffile | awk '{print $1}'`
  # if MD5 checksum has changed, restart the DHCP server
  if [ $md5_res != $md5_new ]; then
    /etc/rc.d/init.d/dhcpd restart
    echo "Restarted DHCP daemon"
    md5_res = $md5_new
  fi
# no 'sleep' here because this script is time critical
done


Next Previous Contents