Angus am Montag, 27. Februar 2006 08.25: > Hi all, > > > > I am having some problems filling a variable based on the contents of a > dhcpd.leases file. All I want at this time is the hostname and ip address. > My eventual goal is to create hash of hashes with this information but for > now I just want to read in the file and see that I have defined my > variables correctly. I am able to get the IP address but the $hostname > variable is always undefined. The syntax for any given host in a leases > file looks like this: > > > > lease 10.10.97.207 { > > starts 2 2005/12/20 16:10:51; > > ends 2 2005/12/20 20:10:51; > > tstp 2 2005/12/20 20:10:51; > > binding state free; > > hardware ethernet 00:0b:97:2b:ea:fe; > > uid "\001\000\013\227+\352\376"; > > client-hostname "HOST1"; > > } > > > > Here is what I have so far. > > > > #!/usr/bin/perl > > # > > use strict; > > use warnings; > > > > my $dhcp_data = "dhcpd.leases"; > > > > my %dhcpd; > > my $ip; > > my $hostname; > > > > { > > open (DHCPD, $dhcp_data) || die "Can't open $dhcp_data $!\n"; > > > > while (my $line = <DHCPD>) { > > next if ($line =~ /^\s*$/ or # blank line > > $line =~ /^\s*#/ ); > > > > if ($line =~ /^lease (\d+\.\d+\.\d+\.\d+)/) { > > $ip = $1; } > > elsif ($line =~ /^client-hostname/) { > > $hostname = $1; } > > else {next;}; > > print "I found IP:$ip\n"; > > print "I found Hostname: $hostname\n"; > > } > > }
Here is a way to process one lease { } after another, with the possibility to extract every field you want. I think it is easy to read, understand, and alter. ===== #!/usr/bin/perl use strict; use warnings; local $/="}\n"; # <<<<< look here! while (my $record=<DATA>) { #print "*** $record ***"; # for debugging record extracting my ($lease)=$record=~/lease\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/; my ($binding_state)=$record=~/^\s+binding\s+state\s+(\w+)/m; my ($client_hostname)=$record=~/^\s+client-hostname\s+"([\w.-_]+)"/m; print "lease '$lease' (host '$client_hostname') has ". "binding state '$binding_state'\n"; } __DATA__ lease 10.10.97.207 { starts 2 2005/12/20 16:10:51; ends 2 2005/12/20 20:10:51; tstp 2 2005/12/20 20:10:51; binding state free; hardware ethernet 00:0b:97:2b:ea:fe; uid "\001\000\013\227+\352\376"; client-hostname "HOST1"; } lease 10.10.97.208 { starts 2 2005/12/20 16:10:51; ends 2 2005/12/20 20:10:51; tstp 2 2005/12/20 20:10:51; binding state free; hardware ethernet 00:0b:97:2b:ea:fe; uid "\001\000\013\227+\352\376"; client-hostname "HOST2"; } ===== This prints out: lease '10.10.97.207' (host 'HOST1') has binding state 'free' lease '10.10.97.208' (host 'HOST2') has binding state 'free' hth, Hans -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>