Regex and detection on dhcpd.conf file

Discussion in 'Perl Misc' started by MaXX, Apr 12, 2005.

  1. MaXX

    MaXX Guest

    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
    MaXX, Apr 12, 2005
    #1
    1. Advertising

  2. MaXX

    Joe Smith Guest

    MaXX wrote:

    > 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
    Joe Smith, Apr 12, 2005
    #2
    1. Advertising

  3. MaXX

    Anno Siegel Guest

    MaXX <> wrote in comp.lang.perl.misc:
    > 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
    Anno Siegel, Apr 12, 2005
    #3
  4. MaXX wrote:
    > 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;
    }
    }

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Apr 12, 2005
    #4
  5. MaXX

    MaXX Guest

    Josef Moellers <> wrote in message news:<d3g5ak$eu4$-siemens.com>...
    > MaXX wrote:
    > > Hi,
    > >=20
    > > 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".
    > >=20
    > >=20
    > > So, my file is composed : =20
    > >=20
    > > 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...
    > >=20
    > > 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 =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.
    MaXX, Apr 12, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve

    Apache conf file parser

    Steve, Nov 17, 2004, in forum: Python
    Replies:
    0
    Views:
    364
    Steve
    Nov 17, 2004
  2. Hugh
    Replies:
    4
    Views:
    433
    CBFalconer
    Jun 3, 2005
  3. Replies:
    3
    Views:
    734
    Reedick, Andrew
    Jul 1, 2008
  4. MegaC
    Replies:
    3
    Views:
    472
    Brian Helterline
    May 24, 2004
  5. Myron

    can I get a new httpd.conf file

    Myron, Feb 8, 2010, in forum: Perl Misc
    Replies:
    19
    Views:
    279
    Steve C
    Feb 10, 2010
Loading...

Share This Page