tombmbdil has asked for the wisdom of the Perl Monks concerning the following question:
I am mainly looking for trouble-shooting tips and methods. The code below reads in a dhcpd.conf file, gathers MAC addresses, looks up the current ip for those devices by parsing dhcpd.leases, and then updates the comments in dhcpd.conf and a LDAP database (although I haven't implemented the database portion yet).
My problems seem to be with the line where I read dhcpd.conf into a variable called $dhcpdconf... if I do this then the program hangs up in the middle of parsing dhcpd.leases. If I truncate dhcpd.conf down to only a few lines (from its 2834 lines in original form), everything runs smoothly.
If I try to read in dhcpd.conf via a system call to cat, things hang up in the same way. When I wrote a seperate script that only had: $dhcpdconf = `cat dhcpd.conf`; print $dhcpdconf; everything worked.
The dhcpd.conf and leases code don't seem to be related, and I'm not sure how to go about tracking down the problem. What tools and techniques have others used to track down problems such as this.. or does someone see something I'm obviously doing incorrectly?
PS- When I say the script 'hangs up' I mean that the process keeps running without seeming to do anything.
#!/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 (<DHCPDCONF>) { $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 (<LEASES>) { $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$d +evices{$device}$2/ig; } print "Marker 3\n"; open (DHCPDCONF, ">dhcpd.conf") or die; print DHCPDCONF "$dhcpdconf"; close DHCPDCONF; ######## Update LDAP database # not implemented yet.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: script hangs up when reading in file
by Gilimanjaro (Hermit) on Jan 17, 2003 at 00:54 UTC | |
by tombmbdil (Beadle) on Jan 17, 2003 at 17:13 UTC | |
by Gilimanjaro (Hermit) on Jan 20, 2003 at 11:35 UTC | |
|
Re: script hangs up when reading in file
by chromatic (Archbishop) on Jan 16, 2003 at 23:19 UTC | |
by tombmbdil (Beadle) on Jan 16, 2003 at 23:56 UTC | |
|
Re: script hangs up when reading in file
by derby (Abbot) on Jan 17, 2003 at 15:41 UTC | |
by tombmbdil (Beadle) on Jan 17, 2003 at 17:18 UTC |