Regex and detection on dhcpd.conf file

M

MaXX

Hi,

I try to extract a information (mac address) in a file dhcpd.conf. I
don't manage to detect the field "hardware ethernet" and extract the
values which follow (values hex). But, i know, for example, "toto" in
the field "host".


So, my file is composed :

host toto {
hardware ethernet 00:05:5b:22:55:96;
fixed-address 192.168.21.55;
}
host titi {
hardware ethernet 00:B0:DD:D7:FF:78;
fixed-address 192.168.21.56;
}
etc...

if you have a idea...

Thanks
 
J

Joe Smith

MaXX said:
I try to extract a information (mac address) in a file dhcpd.conf.

perl -lne '/host\s+(\S+)/ and $host=$1; /ethernet\s+(\S+)/ and print
"$host $1"' dhcpd.conf

-Joe
 
A

Anno Siegel

MaXX said:
Hi,

I try to extract a information (mac address) in a file dhcpd.conf. I

What information? Please be specific.
don't manage to detect the field "hardware ethernet" and extract the
values which follow (values hex). But, i know, for example, "toto" in
the field "host".


So, my file is composed :

host toto {
hardware ethernet 00:05:5b:22:55:96;
fixed-address 192.168.21.55;
}
host titi {
hardware ethernet 00:B0:DD:D7:FF:78;
fixed-address 192.168.21.56;
}

If you give example data you should always describe the variability
in the data. The details of a solution depend heavily on that. Just
a few questions your example raises:

- Are there always four lines to the record?
- Is the sequence always "hardware ethernet ..." followed by "fixed-address"?
- Is "host" always at the beginning of a line?
- Are the "middle lines" always indented by 8 blanks? Could there be tabs?
- Are the words on a line always separated by exactly one blank? Could
there be tabs?

....and so on.

Making some assumptions about the answers, here is one solution (replace
"DATA" with a filehandle to your dhcpd.conf):

my $host = 'toto';

$/ = "}\n"; # should be localized in a real program
while ( <DATA> ) {
if ( /^host $host/ ) {
my ( $mac_addr) = /hardware ethernet ([0-9a-f:]+)/i;
print "$host: $mac_addr\n";
}
}

Anno
 
J

Josef Moellers

MaXX said:
Hi,

I try to extract a information (mac address) in a file dhcpd.conf. I
don't manage to detect the field "hardware ethernet" and extract the
values which follow (values hex). But, i know, for example, "toto" in
the field "host".


So, my file is composed :

host toto {
hardware ethernet 00:05:5b:22:55:96;
fixed-address 192.168.21.55;
}
host titi {
hardware ethernet 00:B0:DD:D7:FF:78;
fixed-address 192.168.21.56;
}
etc...

if you have a idea...

Read the file line-by-line looking for the host line.
If you find the host line, start looking for the hardware ethernet line.

my $hostfound = 0;
my $host = 'toto';
while (<DHCPD_CONF>) {
if (!$hostfound) {
if (/^\s*host\s+$host\b/) {
$hostfound = 1;
}
next;
} elsif (/^\s+hardware\s+ethernet\s+(\S+)\s*;/) {
print "$1\n";
last;
} elsif (/^\s*}/) {
last;
}
}
 
M

MaXX

Josef Moellers said:
Read the file line-by-line looking for the host line.
If you find the host line, start looking for the hardware ethernet line.

my $hostfound =3D 0;
my $host =3D 'toto';
while (<DHCPD_CONF>) {
if (!$hostfound) {
if (/^\s*host\s+$host\b/) {
$hostfound =3D 1;
}
next;
} elsif (/^\s+hardware\s+ethernet\s+(\S+)\s*;/) {
print "$1\n";
last;
} elsif (/^\s*}/) {
last;
}
}

--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett



Thanks! I m going to try this method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top