| 1 |
#!/usr/bin/perl -w |
| 2 |
# Steven Shiau <steven _at_ clonezilla org> |
| 3 |
# License: GPL |
| 4 |
# Description: To parse the dhcpd.conf as a HOSTNAME IP MAC table. |
| 5 |
# The reference is like: |
| 6 |
#$VAR1 = { |
| 7 |
# 'mdk101-201' => { |
| 8 |
# 'IP' => '192.168.219.1', |
| 9 |
# 'MAC' => '00:50:56:00:01:01' |
| 10 |
# }, |
| 11 |
# 'mdk101-102' => { |
| 12 |
# 'IP' => '192.168.199.2', |
| 13 |
# 'MAC' => '00:50:56:00:00:02' |
| 14 |
# }, |
| 15 |
# 'mdk101-101' => { |
| 16 |
# 'IP' => '192.168.199.1', |
| 17 |
# 'MAC' => '00:50:56:00:00:01' |
| 18 |
# } |
| 19 |
# }; |
| 20 |
|
| 21 |
use strict; |
| 22 |
use Data::Dumper; |
| 23 |
|
| 24 |
if ($#ARGV != 0) { |
| 25 |
print "You must specify the output file! Program terminated!!!\n"; |
| 26 |
exit(1); |
| 27 |
} |
| 28 |
|
| 29 |
my $IP_MAC_FILE=shift(@ARGV); |
| 30 |
my ($HOSTS, $ihost); |
| 31 |
my $dhcpd_conf=""; |
| 32 |
|
| 33 |
unlink $IP_MAC_FILE if -f $IP_MAC_FILE; |
| 34 |
open(OUT, ">$IP_MAC_FILE") || die "Can't open file: $IP_MAC_FILE\n"; |
| 35 |
if ((-f "/etc/debian_version")) { |
| 36 |
# Debian |
| 37 |
if ((-f "/etc/dhcp/dhcpd.conf")) { |
| 38 |
$dhcpd_conf = "/etc/dhcp/dhcpd.conf"; |
| 39 |
}elsif ((-f "/etc/dhcp3/dhcpd.conf")) { |
| 40 |
$dhcpd_conf = "/etc/dhcp3/dhcpd.conf"; |
| 41 |
} |
| 42 |
}else{ |
| 43 |
# RH-like or SUSE |
| 44 |
if ((-f "/etc/dhcp/dhcpd.conf")) { |
| 45 |
$dhcpd_conf = "/etc/dhcp/dhcpd.conf"; |
| 46 |
}elsif((-f "/etc/dhcpd.conf")) { |
| 47 |
$dhcpd_conf = "/etc/dhcpd.conf"; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ("$dhcpd_conf" eq "") { |
| 52 |
print "dhcpd.conf not found! Program terminated!!!\n"; |
| 53 |
exit(1); |
| 54 |
} |
| 55 |
|
| 56 |
sub parse_dhcpd_conf { |
| 57 |
my (%host_list, $host); |
| 58 |
open(IN, $dhcpd_conf) || die "Can't open file: $!"; |
| 59 |
while (<IN>) { |
| 60 |
next unless (/^[[:space:]]*host .*{/i || /^[[:space:]]*hardware ethernet/i || /^[[:space:]]*fixed-address/i); |
| 61 |
my $line = $_; |
| 62 |
chomp $line; |
| 63 |
if ($line =~ /^[[:space:]]*host .*{/i) { |
| 64 |
$host = (split(' ', $line))[1]; |
| 65 |
next; |
| 66 |
} |
| 67 |
if ($line =~ /^[[:space:]]*hardware ethernet .*$/i) { |
| 68 |
my $mac = (split(' ', $line))[2]; |
| 69 |
$mac =~ s/;$//; |
| 70 |
# We convert all the MAC to lowercase, which is used in pxelinux config filename. |
| 71 |
$mac = lc($mac); |
| 72 |
$host_list{$host}{"MAC"} = $mac; |
| 73 |
next; |
| 74 |
} |
| 75 |
if ($line =~ /^[[:space:]]*fixed-address .*$/i) { |
| 76 |
my $ip = (split(' ', $line))[1]; |
| 77 |
$ip =~ s/;$//; |
| 78 |
$host_list{$host}{"IP"} = $ip; |
| 79 |
next; |
| 80 |
} |
| 81 |
} |
| 82 |
close(IN); |
| 83 |
#print Dumper(\%host_list); |
| 84 |
return (\%host_list); |
| 85 |
} |
| 86 |
|
| 87 |
# make the reference. |
| 88 |
$HOSTS = parse_dhcpd_conf(); |
| 89 |
|
| 90 |
# output the results |
| 91 |
print OUT "# Generated by DRBL script parse_dhcpd_conf\n"; |
| 92 |
print OUT "# HOSTNAME IP MAC_ADDRESS\n"; |
| 93 |
foreach my $ihost ( sort keys(%{$HOSTS}) ) { |
| 94 |
print OUT "$ihost $HOSTS->{$ihost}->{IP} $HOSTS->{$ihost}->{MAC}\n"; |
| 95 |
} |