Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

phpmyadmin fehler rechte unerlaubte mischung, WWWXXXApc, lilo trying to map files from unnamed device 0x0000", Wwwxxxapc, CAM::PDF::Renderer::TextF Fragen, adodb.recordset error '800a0cc1', php save mails as eml, WWWXXXAPC, sqlexpress database file auto-creation error, sqlexpress database file auto-creation error

Links

XODOX
Impressum
Datenschutz

#1: problems parsing a DHCP.leases file.

Posted on 2006-02-27 08:25:13 by angus

------=_NextPart_000_0023_01C63B2B.E4DF26A0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

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";

}

}



Thanks,



-angus


------=_NextPart_000_0023_01C63B2B.E4DF26A0--

Report this message

#2: Re: problems parsing a DHCP.leases file.

Posted on 2006-02-27 09:07:42 by Jeff Pang

Hi,
here is wrong:

elsif ($line =~ /^client-hostname/) {

$hostname = $1; }

for the line like that:

client-hostname "HOST1";

I think maybe you should do:

elsif ($line =~ /^\s*client-hostname\s+\"(.*?)\"){
$hostname = $1;}


HTH.

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Report this message

#3: Re: problems parsing a DHCP.leases file.

Posted on 2006-02-27 09:12:57 by The Ghost

--Apple-Mail-1--487979371
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

you need to match something.

You probably meant:

elsif ($line =~ /^client-hostname\s+"([a-z0-9\-\.]+)"/i) {

On the other hand, there is probably a module that already deals with
this and would save you even more coding time.

Ryan




On Feb 27, 2006, at 1:25 AM, Angus wrote:

> elsif ($line =~ /^client-hostname/) {
>
> $hostname = $1; }


--Apple-Mail-1--487979371--

Report this message

#4: RE: problems parsing a DHCP.leases file.

Posted on 2006-02-27 09:26:41 by angus

Ryan,

Thanks for the tip however, in this case what I am trying to do (I think) is
find a line that starts with "client-hostname" then match my variable
$hostname to the second thing that the regex matches in that line $1. It
works for the ip address but fails for the hostname. I did try to regex
match but it still returns an undef variable...

-angus

-----Original Message-----
From: The Ghost [mailto:ghost@madisonip.com]
Sent: Monday, February 27, 2006 12:13 AM
To: Angus
Cc: beginners@perl.org
Subject: Re: problems parsing a DHCP.leases file.

you need to match something.

You probably meant:

elsif ($line =~ /^client-hostname\s+"([a-z0-9\-\.]+)"/i) {

On the other hand, there is probably a module that already deals with
this and would save you even more coding time.

Ryan




On Feb 27, 2006, at 1:25 AM, Angus wrote:

> elsif ($line =~ /^client-hostname/) {
>
> $hostname = $1; }



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Report this message

#5: Re: problems parsing a DHCP.leases file.

Posted on 2006-02-27 09:45:38 by The Ghost

When I change to :
....
elsif ($line =~ /^\s+?client-hostname\s+"([a-z0-9\-\.]+)"/i) {
....

I found IP:10.10.97.207
I found Hostname:
I found IP:10.10.97.207
I found Hostname: HOST1

Seems you have some space there. Also, you probably want to move
your my $hostname and my $ip statements. If a hostname line wasn't
found, it would show up as the previously defined hostname (same with
IP)!


Anyway, that should help, it DOES find it - this should help you
solve the problem.

Ryan

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Report this message

#6: Re: problems parsing a DHCP.leases file.

Posted on 2006-02-27 09:52:38 by security.department

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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Report this message

#7: RE: problems parsing a DHCP.leases file.

Posted on 2006-03-02 09:06:55 by angus

Hans,

This script works really well but, I am a bit confused on what you are doing
with this: local $/="}\n"; I have not seen local used much as I thought it
was replaced by "my". It almost looks like you are defining the end of each
lease entry with a closing curly brace and a new line. Does the dollar sign
indicate that is the end of the input?

As for the regex matches the first two make sense to me but I am a bit
confused on the third one
my ($client_hostname)=$record=~/^\s+client-hostname\s+"([\w.-_] +)"/m

I can see that we are creating a variable called $client_hostname which is
defined by a match to $record which is feed in by the filehandle. I see
that we are searching for a line starting with one or more spaces followed
by client-hostname then one or more spaces followed by one word character
and anything else but what does the -_ do? And what does the m on the
outside do?

Thanks again for the example it is really interesting and has helped me.

-angus


-----Original Message-----
From: Hans Meier (John Doe) [mailto:security.department@tele2.ch]
Sent: Monday, February 27, 2006 12:53 AM
To: beginners@perl.org
Subject: Re: problems parsing a DHCP.leases file.

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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Report this message

#8: Re: problems parsing a DHCP.leases file.

Posted on 2006-03-02 10:29:50 by krahnj

[ Please do not top-post. Please remove any quoted text that is not relevant
to your post. ]


Angus wrote:
> From: Hans Meier (John Doe)
>>
>> 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";
>> }
>
> This script works really well but, I am a bit confused on what you are doing
> with this: local $/="}\n";

$/ is the Input Record Separator variable.

perldoc perlvar

> I have not seen local used much as I thought it
> was replaced by "my".

It has been for user defined variables however you still have to use local for
Perl's "special" variables like $/.

> It almost looks like you are defining the end of each
> lease entry with a closing curly brace and a new line. Does the dollar sign
> indicate that is the end of the input?

No, the dollar sign indicates that / is the name of a scalar variable.

> As for the regex matches the first two make sense to me but I am a bit
> confused on the third one
> my ($client_hostname)=$record=~/^\s+client-hostname\s+"([\w.-_] +)"/m
>
> I can see that we are creating a variable called $client_hostname which is
> defined by a match to $record which is feed in by the filehandle. I see
> that we are searching for a line starting with one or more spaces followed
> by client-hostname then one or more spaces followed by one word character
> and anything else but what does the -_ do?

The hythen (-) in a character class defines a range of characters unless it is
at the beginning or end of the character class so '.-_' is the range of
characters starting at '.' and ending at '_'. That is probably a mistake but
I would have to check the RFCs to confirm that. Hans probably meant '[\w._-]'
instead?

> And what does the m on the
> outside do?

The /m option means that ^ will match at the beginning of a line inside the
string in $record instead of at the beginning of $record.



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Report this message