[Fai-commit] r5977 - in branches/stable/3.4: . bin debian

Michael Prokop mika at alioth.debian.org
Tue Aug 17 12:53:35 UTC 2010


Author: mika
Date: 2010-08-17 12:53:34 +0000 (Tue, 17 Aug 2010)
New Revision: 5977

Added:
   branches/stable/3.4/bin/dhcp-edit
Modified:
   branches/stable/3.4/Makefile
   branches/stable/3.4/debian/changelog
   branches/stable/3.4/debian/fai-server.install
   branches/stable/3.4/debian/fai-server.manpages
Log:
dhcp-edit: new command that adds or removes entries to/from dhcpd.conf


Signed-off-by: Michael Prokop <mika at grml.org>

Modified: branches/stable/3.4/Makefile
===================================================================
--- branches/stable/3.4/Makefile	2010-08-17 12:53:23 UTC (rev 5976)
+++ branches/stable/3.4/Makefile	2010-08-17 12:53:34 UTC (rev 5977)
@@ -5,7 +5,7 @@
 export DOCDIR = $(shell pwd)/debian/fai-doc/usr/share/doc/fai-doc
 LIBDIR = $(DESTDIR)/usr/lib/fai
 SHAREDIR = $(DESTDIR)/usr/share/fai
-USRSBIN_SCRIPTS = make-fai-nfsroot fai-setup fcopy ftar install_packages fai-chboot faimond fai-cd fai setup_harddisks faireboot fai-statoverride setup-storage
+USRSBIN_SCRIPTS = make-fai-nfsroot fai-setup fcopy ftar install_packages fai-chboot faimond fai-cd fai setup_harddisks faireboot fai-statoverride setup-storage dhcp-edit
 
 USRBIN_SCRIPTS = fai-class fai-do-scripts fai-mirror fai-debconf device2grub policy-rc.d.fai ainsl faimond-gui
 
@@ -27,6 +27,7 @@
 	mkdir -p $(DESTDIR)/usr/{sbin,bin} $(DESTDIR)/usr/lib/fai $(DESTDIR)/etc/fai/apt
 	mkdir -p $(DESTDIR)/etc/{init,init.d} $(DESTDIR)/usr/share/fai/{pixmaps,setup-storage}
 	install man/* $(DESTDIR)/man
+	pod2man -c '' -r '' -s8 bin/dhcp-edit > $(DESTDIR)/man/dhcp-edit.8
 	$(MAKE) -C doc install
 	-install $(libfiles) $(LIBDIR)
 	install lib/setup-storage/* $(SHAREDIR)/setup-storage

Added: branches/stable/3.4/bin/dhcp-edit
===================================================================
--- branches/stable/3.4/bin/dhcp-edit	                        (rev 0)
+++ branches/stable/3.4/bin/dhcp-edit	2010-08-17 12:53:34 UTC (rev 5977)
@@ -0,0 +1,190 @@
+#! /usr/bin/perl
+
+# $Id$
+#*********************************************************************
+#
+# dhcp-edit -- managing dhcpd entries made easy
+#
+# This script is part of FAI (Fully Automatic Installation)
+# Copyright (C) 2010 Thomas Lange, lange at informatik.uni-koeln.de
+# Universitaet zu Koeln
+#
+#*********************************************************************
+# 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.
+#*********************************************************************
+
+# TODO
+# -q quiet: do not print error if host/mac entry not found, exit code 0
+
+$version="Version 1.1, 24-june-2010";
+$dhcpdconf="/etc/dhcp3/dhcpd.conf";
+
+our ($opt_p,$opt_d,$opt_h,$opt_n,$opt_r);
+
+use Pod::Usage;
+use Getopt::Std;
+
+getopts('p:dhnr') || pod2usage(-msg => "edit-dhcp, $version", -verbose => 2);
+$opt_h && pod2usage(-msg => "edit-dhcp, $version",-verbose => 1);
+($hostname,$mac,$ip)= @ARGV;
+$hostname || pod2usage(-msg => "edit-dhcp, $version",-verbose => 1);
+$ip && merror(4,"$ip is not a correct IP address") unless $ip =~ /^[.0-9]{7,15}$/i;
+
+read_dhcpd_conf();
+
+if ($opt_r) {
+
+  $mac=$hostname;
+  # create emty entry, remove entry
+  $found=0;
+
+  # set flag if an entry was found. print warning if entry not found
+  foreach (@dhcpd) {
+    next if /^\s*#/;  # do not change comments
+    do {$_="XXX ENTRY DELETED XXX\n";$found++} if m/host\s+$hostname\b.+hardware\s+ethernet.+;/;
+    do {$_="XXX ENTRY DELETED XXX\n";$found++} if m/host\s+.+hardware\s+ethernet\s+$mac[\s+;]/i;
+  }
+  merror(6,"Entry $hostname can not be removed. Not found.\n") unless $found;
+  print "$found entry/entries removed.\n" if $found;
+
+} else {
+
+  $mac || merror(5,"Please specify hostname and MAC address.");
+  merror(4,"$hostname is not a correct host name") unless $hostname =~ /^[.0-9a-z-]+$/i;
+  merror(4,"$mac is not a correct MAC address") unless $mac =~ /^([0-9a-f]{1,2}(:|$)){6}$/i;
+  # grep all lines if the entry already exists
+  foreach (@dhcpd) {
+    next if /^\s*#/;  # do not change comments
+    merror(7,"$hostname already exists in dhcpd.conf") if m/host\s+$hostname\b/;
+    merror(7,"MAC address $mac already exists in dhcpd.conf") if m/hardware\s+ethernet\s+$mac[\s+;]/i;
+  }
+}
+
+# check if executed as root
+merror(3,"Terminated. $0 can only be run as root.") unless ($< == 0);
+add_entry($hostname,$ip) unless $opt_r;
+write_dhcpd();
+do {
+  if ($opt_d) {
+    print "DRY RUN. Did not restart dhcp daemon.";
+    exit 0;
+  }
+  print qx#/etc/init.d/dhcp3-server restart#
+} unless $opt_n;
+
+exit 0;
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+sub merror {
+
+  $error = shift;
+  warn "$0 ERROR: @_\n";
+  exit $error;
+}
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+sub read_dhcpd_conf {
+
+  # read the whole dhcpd.conf
+  open(DHCP,"$dhcpdconf") || die "Can't read $dhcpdconf. $!\n";
+  @dhcpd = <DHCP>;
+  close(DHCP);
+}
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+sub add_entry {
+
+  my @new;
+
+  my ($hostname,$ip) = @_;
+  $ip=$hostname unless $ip;
+
+  # add new entry before line matching $opt_n
+  foreach (@dhcpd) {
+    if ($_ =~ /$opt_p/o) {
+      push @new, "host $hostname {hardware ethernet $mac;fixed-address $ip;}\n";
+      print "Entry added: host $hostname {hardware ethernet $mac;fixed-address $ip;}\n";
+    }
+    push @new,$_;
+  }
+  @dhcpd = @new;
+}
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+sub write_dhcpd {
+
+  if ($opt_d) {
+    print "DRY RUN. Nothing changed.";
+    return;
+  }
+
+  @dhcpd = grep(!/^XXX ENTRY DELETED XXX\n$/, @dhcpd);
+#  print @dhcpd;
+  open(DHCP," >$dhcpdconf") || die "Can't write $dhcpdconf. $!\n";
+  print DHCP @dhcpd;
+  close(DHCP);
+}
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+__END__
+
+=head1 NAME
+
+dhcp-edit - add or and remove entries to/from dhcpd.conf
+
+=head1 SYNOPSIS
+
+dhcp-edit [OPTION] HOST MAC [IP]
+
+=head1 DESCRIPTION
+
+Add a new host entry to dhcpd.conf or remove an existing entry.
+Additionally restart DHCP daemon.
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<-d>
+
+Dry run. Do not change files.
+
+=item B<-h>
+
+Print help.
+
+=item B<-n>
+
+Do not restart DHCP daemon.
+
+=item B<-p> PATTERN
+
+Add new entry before line matching PATTERN
+
+=item B<-r> HOST|MAC
+
+Remove entry contain HOST or MAC address.
+
+=back
+
+=head1 EXAMPLES
+
+dhcp-edit host mac
+
+   Add entry using host and mac address using a fixed IP address. You
+   have to define the IP address in /etc/hosts or similar service.
+
+
+dhcp-edit host mac ip
+
+   Add entry using host and mac address using the numerical IP address.
+
+
+dhcp-edit -r hostname|mac
+
+   Remove line containing this hostname or mac address.
+
+=head1 COPYRIGHT
+
+This program is Copyright (C) 2010 by Thomas Lange <lange at informatik.uni-koeln.de>
+
+=cut


Property changes on: branches/stable/3.4/bin/dhcp-edit
___________________________________________________________________
Added: svn:executable
   + *

Modified: branches/stable/3.4/debian/changelog
===================================================================
--- branches/stable/3.4/debian/changelog	2010-08-17 12:53:23 UTC (rev 5976)
+++ branches/stable/3.4/debian/changelog	2010-08-17 12:53:34 UTC (rev 5977)
@@ -1,6 +1,7 @@
-fai (3.4~beta1) unstable; urgency=low
+fai (3.4~beta2) unstable; urgency=low
 
   [ Thomas Lange ]
+  * dhcp-edit: new command that adds or removes entries to/from dhcpd.conf
   * make-fai-nfsroot: fix typo
   * mkramdisk: use exit in main routine (closes: #583289)
   * ainsl: exit with 0 if line already exists in the file

Modified: branches/stable/3.4/debian/fai-server.install
===================================================================
--- branches/stable/3.4/debian/fai-server.install	2010-08-17 12:53:23 UTC (rev 5976)
+++ branches/stable/3.4/debian/fai-server.install	2010-08-17 12:53:34 UTC (rev 5977)
@@ -5,6 +5,7 @@
 usr/sbin/fai-setup
 usr/sbin/faimond
 usr/sbin/make-fai-nfsroot
+usr/sbin/dhcp-edit
 usr/bin/fai-mirror
 usr/share/fai/pixmaps/*
 usr/share/fai/menu.lst

Modified: branches/stable/3.4/debian/fai-server.manpages
===================================================================
--- branches/stable/3.4/debian/fai-server.manpages	2010-08-17 12:53:23 UTC (rev 5976)
+++ branches/stable/3.4/debian/fai-server.manpages	2010-08-17 12:53:34 UTC (rev 5977)
@@ -5,3 +5,4 @@
 debian/tmp/man/faimond.8
 debian/tmp/man/faimond-gui.1
 debian/tmp/man/make-fai-nfsroot.8
+debian/tmp/man/dhcp-edit.8




More information about the Fai-commit mailing list