Debian Codegraph @

bin/dhcp-edit

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