#!/usr/bin/perl use strict; ##################################################### # Update MAC-IP pairings for all registered devices # ##################################################### my %devices; my $dhcpdconf; # Open dhcpd.conf and initialize the %devices hash with all # registered modems and cpes open (DHCPDCONF, "dhcpd.conf"); foreach my $line () { $devices{lc($1)} = '' if ($line =~ /#.*#.*#.*# Modem MAC: (\w+:\w+:\w+:\w+:\w+:\w+)/ or $line =~ /subclass ".*" 1:(\w+:\w+:\w+:\w+:\w+:\w+)/); # read dhcpd.conf into a variable for later substitutions $dhcpdconf .= $line; # <-- This is the line causing trouble... # the script does not hang up later when # this line is commented out. } close DHCPDCONF; # open leases file and match mac addresses in %devices with active ips open (LEASES, "dhcpd.leases"); my ($current_ip, $state); foreach my $line () { $current_ip = $1 and next if $line =~ /^\s*lease (\d+.\d+.\d+.\d+)/; $state = $1 and next if $line =~ /^\s*binding state (\w+)/; $devices{lc($1)} = $current_ip and print lc($1) . " - " . $devices{lc($1)} . "\n" and next if ($line =~ /^\s*hardware ethernet (\w+:\w+:\w+:\w+:\w+:\w+)/ and $state eq 'active' and exists $devices{lc($1)}); } # <- Hangs up inside the loop, never makes it to Marker 1 print "Marker 1\n"; close LEASES; # alternative method of reading dhcpd.conf #$dhcpdconf = `cat dhcpd.conf`; print "Marker 2\n"; # <--- hangs up right here with `cat dhcpd.conf` method. # substitute old ip with current value foreach my $device (keys %devices) { print "."; $dhcpdconf =~ s/(#.*#.*# Modem IP: ).*( # Modem MAC: $device)/$1$devices{$device}$2/ig; } print "Marker 3\n"; open (DHCPDCONF, ">dhcpd.conf") or die; print DHCPDCONF "$dhcpdconf"; close DHCPDCONF; ######## Update LDAP database # not implemented yet.