#!/usr/bin/perl # # create a template for /etc/bootptab # read logfile and create entry for each MAC address # tested with bootpd version 2.4.3 # bootpd needs option -d4 # use ctrl-c to stop # # Copyright (C) 1999 August Hoerandl (hoerandl@elina.htlw1.ac.at) # http://elina.htlw1.ac.at/~hoerandl # # 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., 675 Mass Ave, Cambridge, MA 02139, USA. # change to fit your needs # where to write the new entries $file="bootptab.new"; #where to read $log="/var/log/messages"; #$log="bootp.log"; # subnet $subnet="192.168.1"; # first free ip/name - to be incremented $num=100; $name="a"; #hostname $host="pc409"; #template $template=".$host"; # type of hostname created # $host\$name -> pca, pcb, pcc, ... # $host\$num -> pc1, pc2, pc3, pc4, ... $fullname="$host\$name"; # full line for bootptab # use vars from above + $mac # use \$ for the changing values $line="$fullname:ha=\$mac:ip=$subnet.\$num:tc=$template:"; # no user serviceable parts below # first read all mac addr. already known open(FILE, "$file") || die "can't open $file: $!\n"; while() { next unless /[Hh][Aa]=([0-9a-fA-F]{12,12})/; $mac=$1; print "found mac $mac\n"; $macs{$mac}++; } close (FILE) || die "can't close $file: $!\n"; # now read the log # open(INPUT, "$log") || die "can't open $log: $!\n"; while (1) { while () { next unless /bootpd/; # print; if (/([0-9a-fA-F][0-9a-fA-F]):([0-9a-fA-F][0-9a-fA-F]):([0-9a-fA-F][0-9a-fA-F]):([0-9a-fA-F][0-9a-fA-F]):([0-9a-fA-F][0-9a-fA-F]):([0-9a-fA-F][0-9a-fA-F])/ ) { $mac="$1$2$3$4$5$6"; print; if (! $macs{$mac} ) { $macs{$mac}++; open(FILE, ">>$file") || die "can't open $file: $!\n"; eval "print \"\a$line\n\a\""; eval "print FILE \"$line\n\""; close(FILE)|| die "can't close $file: $!\n"; $num++; $name++; } } } sleep(2); seek(INPUT, 0, 1); }