| 1 |
lange |
5816 |
#! /usr/bin/perl |
| 2 |
|
|
|
| 3 |
|
|
# $Id$ |
| 4 |
|
|
#********************************************************************* |
| 5 |
|
|
# |
| 6 |
|
|
# dhcp-edit -- managing dhcpd entries made easy |
| 7 |
|
|
# |
| 8 |
|
|
# This script is part of FAI (Fully Automatic Installation) |
| 9 |
|
|
# Copyright (C) 2010 Thomas Lange, lange@informatik.uni-koeln.de |
| 10 |
|
|
# Universitaet zu Koeln |
| 11 |
|
|
# |
| 12 |
|
|
#********************************************************************* |
| 13 |
|
|
# This program is free software; you can redistribute it and/or modify |
| 14 |
|
|
# it under the terms of the GNU General Public License as published by |
| 15 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
| 16 |
|
|
# (at your option) any later version. |
| 17 |
|
|
#********************************************************************* |
| 18 |
|
|
|
| 19 |
|
|
# TODO |
| 20 |
|
|
# -q quiet: do not print error if host/mac entry not found, exit code 0 |
| 21 |
|
|
|
| 22 |
lange |
5916 |
$dhcpdconf=(-d "/etc/dhcp") ? "/etc/dhcp/dhcpd.conf" : "/etc/dhcp3/dhcpd.conf"; |
| 23 |
lange |
5816 |
|
| 24 |
lange |
5843 |
$modified=0; # 1 if dhcpd.conf was modified |
| 25 |
lange |
5816 |
our ($opt_p,$opt_d,$opt_h,$opt_n,$opt_r); |
| 26 |
|
|
|
| 27 |
|
|
use Pod::Usage; |
| 28 |
|
|
use Getopt::Std; |
| 29 |
|
|
|
| 30 |
lange |
5872 |
getopts('p:dhnr') || pod2usage(-msg => "edit-dhcp", -verbose => 2); |
| 31 |
|
|
$opt_h && pod2usage(-msg => "edit-dhcp",-verbose => 1); |
| 32 |
lange |
5816 |
($hostname,$mac,$ip)= @ARGV; |
| 33 |
lange |
5872 |
$hostname || pod2usage(-msg => "edit-dhcp",-verbose => 1); |
| 34 |
lange |
5816 |
$ip && merror(4,"$ip is not a correct IP address") unless $ip =~ /^[.0-9]{7,15}$/i; |
| 35 |
|
|
|
| 36 |
|
|
read_dhcpd_conf(); |
| 37 |
|
|
|
| 38 |
|
|
if ($opt_r) { |
| 39 |
|
|
|
| 40 |
|
|
$mac=$hostname; |
| 41 |
lange |
5843 |
# create empty entry, remove entry |
| 42 |
lange |
5816 |
|
| 43 |
|
|
# set flag if an entry was found. print warning if entry not found |
| 44 |
|
|
foreach (@dhcpd) { |
| 45 |
|
|
next if /^\s*#/; # do not change comments |
| 46 |
lange |
5843 |
do {$_="XXX ENTRY DELETED XXX\n";$modified++} if m/host\s+$hostname\b.+hardware\s+ethernet.+;/; |
| 47 |
|
|
do {$_="XXX ENTRY DELETED XXX\n";$modified++} if m/host\s+.+hardware\s+ethernet\s+$mac[\s+;]/i; |
| 48 |
lange |
5816 |
} |
| 49 |
lange |
5843 |
merror(6,"Entry $hostname can not be removed. Not found.\n") unless $modified; |
| 50 |
|
|
print "$modified entry/entries removed.\n" if $modified; |
| 51 |
lange |
5816 |
|
| 52 |
|
|
} else { |
| 53 |
|
|
|
| 54 |
|
|
$mac || merror(5,"Please specify hostname and MAC address."); |
| 55 |
|
|
merror(4,"$hostname is not a correct host name") unless $hostname =~ /^[.0-9a-z-]+$/i; |
| 56 |
|
|
merror(4,"$mac is not a correct MAC address") unless $mac =~ /^([0-9a-f]{1,2}(:|$)){6}$/i; |
| 57 |
|
|
# grep all lines if the entry already exists |
| 58 |
|
|
foreach (@dhcpd) { |
| 59 |
|
|
next if /^\s*#/; # do not change comments |
| 60 |
|
|
merror(7,"$hostname already exists in dhcpd.conf") if m/host\s+$hostname\b/; |
| 61 |
|
|
merror(7,"MAC address $mac already exists in dhcpd.conf") if m/hardware\s+ethernet\s+$mac[\s+;]/i; |
| 62 |
|
|
} |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
|
|
# check if executed as root |
| 66 |
|
|
merror(3,"Terminated. $0 can only be run as root.") unless ($< == 0); |
| 67 |
|
|
add_entry($hostname,$ip) unless $opt_r; |
| 68 |
|
|
write_dhcpd(); |
| 69 |
lange |
5843 |
restart_dhcpd(); |
| 70 |
lange |
5816 |
|
| 71 |
|
|
exit 0; |
| 72 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 73 |
|
|
sub merror { |
| 74 |
|
|
|
| 75 |
|
|
$error = shift; |
| 76 |
|
|
warn "$0 ERROR: @_\n"; |
| 77 |
|
|
exit $error; |
| 78 |
|
|
} |
| 79 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 80 |
|
|
sub read_dhcpd_conf { |
| 81 |
|
|
|
| 82 |
|
|
# read the whole dhcpd.conf |
| 83 |
|
|
open(DHCP,"$dhcpdconf") || die "Can't read $dhcpdconf. $!\n"; |
| 84 |
|
|
@dhcpd = <DHCP>; |
| 85 |
|
|
close(DHCP); |
| 86 |
|
|
} |
| 87 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 88 |
|
|
sub add_entry { |
| 89 |
|
|
|
| 90 |
|
|
my @new; |
| 91 |
|
|
|
| 92 |
|
|
my ($hostname,$ip) = @_; |
| 93 |
|
|
$ip=$hostname unless $ip; |
| 94 |
|
|
|
| 95 |
lange |
5843 |
# if -p was not given |
| 96 |
|
|
unless (defined $opt_p) { |
| 97 |
|
|
$modified=1; |
| 98 |
|
|
push @dhcpd, "host $hostname {hardware ethernet $mac;fixed-address $ip;}\n"; |
| 99 |
|
|
print "Entry added: host $hostname {hardware ethernet $mac;fixed-address $ip;}\n" if $modified; |
| 100 |
|
|
return; |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
|
|
# add new entry before line matching $opt_p |
| 104 |
|
|
# if $opt_p matches multiple times, also the new entry is added multiple times |
| 105 |
lange |
5816 |
foreach (@dhcpd) { |
| 106 |
|
|
if ($_ =~ /$opt_p/o) { |
| 107 |
|
|
push @new, "host $hostname {hardware ethernet $mac;fixed-address $ip;}\n"; |
| 108 |
|
|
print "Entry added: host $hostname {hardware ethernet $mac;fixed-address $ip;}\n"; |
| 109 |
lange |
5843 |
$modified=1; |
| 110 |
lange |
5816 |
} |
| 111 |
|
|
push @new,$_; |
| 112 |
|
|
} |
| 113 |
|
|
@dhcpd = @new; |
| 114 |
|
|
} |
| 115 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 116 |
|
|
sub write_dhcpd { |
| 117 |
|
|
|
| 118 |
|
|
if ($opt_d) { |
| 119 |
|
|
print "DRY RUN. Nothing changed."; |
| 120 |
|
|
return; |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
lange |
5843 |
unless ($modified) { |
| 124 |
|
|
print "Nothing changed."; |
| 125 |
|
|
return; |
| 126 |
|
|
} |
| 127 |
|
|
|
| 128 |
lange |
5816 |
@dhcpd = grep(!/^XXX ENTRY DELETED XXX\n$/, @dhcpd); |
| 129 |
|
|
# print @dhcpd; |
| 130 |
|
|
open(DHCP," >$dhcpdconf") || die "Can't write $dhcpdconf. $!\n"; |
| 131 |
|
|
print DHCP @dhcpd; |
| 132 |
|
|
close(DHCP); |
| 133 |
|
|
} |
| 134 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 135 |
lange |
5843 |
sub restart_dhcpd { |
| 136 |
lange |
5816 |
|
| 137 |
lange |
5843 |
if ($opt_d) { |
| 138 |
|
|
print "DRY RUN. Did not restart dhcp daemon.\n"; |
| 139 |
|
|
} |
| 140 |
|
|
if ($opt_n) { |
| 141 |
|
|
print "Did not restart dhcp daemon.\n"; |
| 142 |
|
|
} |
| 143 |
|
|
unless ($modified) { |
| 144 |
|
|
print "No modifications.\n"; |
| 145 |
|
|
} |
| 146 |
|
|
|
| 147 |
|
|
if ($opt_d || $opt_n || $modified==0) { |
| 148 |
|
|
print "Did not restart dhcp daemon.\n"; |
| 149 |
|
|
return; |
| 150 |
|
|
} |
| 151 |
|
|
|
| 152 |
lange |
5916 |
(-x "/etc/init.d/isc-dhcp-server") and print qx#/etc/init.d/isc-dhcp-server# or |
| 153 |
|
|
print qx#/etc/init.d/dhcp3-server restart#; |
| 154 |
lange |
5843 |
} |
| 155 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 156 |
|
|
|
| 157 |
lange |
5816 |
__END__ |
| 158 |
|
|
|
| 159 |
|
|
=head1 NAME |
| 160 |
|
|
|
| 161 |
|
|
dhcp-edit - add or and remove entries to/from dhcpd.conf |
| 162 |
|
|
|
| 163 |
|
|
=head1 SYNOPSIS |
| 164 |
|
|
|
| 165 |
|
|
dhcp-edit [OPTION] HOST MAC [IP] |
| 166 |
|
|
|
| 167 |
|
|
=head1 DESCRIPTION |
| 168 |
|
|
|
| 169 |
|
|
Add a new host entry to dhcpd.conf or remove an existing entry. |
| 170 |
|
|
Additionally restart DHCP daemon. |
| 171 |
|
|
|
| 172 |
|
|
=head1 OPTIONS |
| 173 |
|
|
|
| 174 |
|
|
=over 8 |
| 175 |
|
|
|
| 176 |
|
|
=item B<-d> |
| 177 |
|
|
|
| 178 |
|
|
Dry run. Do not change files. |
| 179 |
|
|
|
| 180 |
|
|
=item B<-h> |
| 181 |
|
|
|
| 182 |
|
|
Print help. |
| 183 |
|
|
|
| 184 |
|
|
=item B<-n> |
| 185 |
|
|
|
| 186 |
|
|
Do not restart DHCP daemon. |
| 187 |
|
|
|
| 188 |
|
|
=item B<-p> PATTERN |
| 189 |
|
|
|
| 190 |
|
|
Add new entry before line matching PATTERN |
| 191 |
|
|
|
| 192 |
|
|
=item B<-r> HOST|MAC |
| 193 |
|
|
|
| 194 |
|
|
Remove entry contain HOST or MAC address. |
| 195 |
|
|
|
| 196 |
|
|
=back |
| 197 |
|
|
|
| 198 |
|
|
=head1 EXAMPLES |
| 199 |
|
|
|
| 200 |
|
|
dhcp-edit host mac |
| 201 |
|
|
|
| 202 |
|
|
Add entry using host and mac address using a fixed IP address. You |
| 203 |
|
|
have to define the IP address in /etc/hosts or similar service. |
| 204 |
|
|
|
| 205 |
|
|
|
| 206 |
|
|
dhcp-edit host mac ip |
| 207 |
|
|
|
| 208 |
|
|
Add entry using host and mac address using the numerical IP address. |
| 209 |
|
|
|
| 210 |
|
|
|
| 211 |
|
|
dhcp-edit -r hostname|mac |
| 212 |
|
|
|
| 213 |
|
|
Remove line containing this hostname or mac address. |
| 214 |
|
|
|
| 215 |
|
|
=head1 COPYRIGHT |
| 216 |
|
|
|
| 217 |
|
|
This program is Copyright (C) 2010 by Thomas Lange <lange@informatik.uni-koeln.de> |
| 218 |
|
|
|
| 219 |
|
|
=cut |