#!/usr/bin/perl -Tw ##################################################### # # Copyright (C) 2001, Jon Hart # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ################################# # # A makeshift config file generator for dhcpd. # Reads a file containing MAC's and creates valid # host sections for a dhcpd.conf file, and then combines # this with a base config file. The result is a complete # config file for dhcpd. # # Jon Hart (jhart@ccs.neu.edu) # or # (warchild@spoofed.org) # April 26th, 2001 ###################################################### use strict; $ENV{'PATH'} = "/usr/bin:/bin"; # holds the PID of dhcpd my $DHCPD_PID = "`cat /var/run/dhcpd.pid`"; # how dhcpd is started. my $DHCPD_PATH = "/usr/sbin/dhcpd"; my $DHCPD_CMD = "$DHCPD_PATH -q -cf /var/adm/dhcpd.conf de0"; # where the dhcpd config will live. # we will write to this later. my $DHCP_CONFIG = "/var/adm/dhcpd.conf"; # a base config file that'll be merged with the leases # to ultimately form the final config file. # we won't write to this, only read from it. my $DHCP_CONFIG_BASE = "/var/adm/dhcpd.conf.base"; # flat text file to hold name/email/MAC pairs. # just a flat text file to be edited by hand. my $DHCP_CONFIG_LEASES = "/var/adm/dhcp.leases"; # how many hosts get added to the config file my $hostnum; # the final file we will write to. open(FINAL, "> $DHCP_CONFIG"); open(BASE, "$DHCP_CONFIG_BASE"); while() { print FINAL; } close(BASE); # create host entries for the registered MAC's open(LEASES, "$DHCP_CONFIG_LEASES"); while () { my $record = $_; if ( /^#/ ) { next; } chomp; $hostnum++; # rather than generate hostnames by hand, # simply count how man registered MAC's we've # had and hack together a name. # The name will look like "CCSWireless1" my $hostname = "CCSWireless".$hostnum; my ($mac, $email, $name) = split(/,/, $record); print FINAL "host $hostname { hardware ethernet $mac; }\n"; } print "Wrote $hostnum hosts to the config file.\n"; close(LEASES); close(FINAL); # big deal, we wrote a config file. # Now let dhcpd know whats up. if ( -f "/var/run/dhcpd.pid" ) { my $try = system("kill -9 $DHCPD_PID"); if ( $try != 0 ) { print "Could not kill dhcpd: $?\n"; } elsif ( $try == 0 ) { print "Killed dhcpd.\n"; } } if ( -x "$DHCPD_PATH" ) { my $try = system("$DHCPD_CMD"); if ( $try !=0 ) { print "Could not start dhcpd: $?\n"; } elsif ($try == 0 ) { print "Started dhcpd.\n"; } }