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