Hello fellow monks. I'm relatively new to perl and have been lurking in the monastery for a while. This will be my first 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.

In reply to script hangs up when reading in file by tombmbdil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.