Link to home
Start Free TrialLog in
Avatar of richsark
richsarkFlag for United States of America

asked on

parse through a conf file

HI,

Below is a script that group0 helped me with.

Some how, the output is giving me too many of these

        }

    }
}

# DHCP_GROUP: SEC
{

I need to have it like so, hence remove the brackets


# DHCP_GROUP: SEC

I cant seem to figure out how to make this script do it


Thanks
Avatar of richsark
richsark
Flag of United States of America image

ASKER

Here is the script: and the raw file.

The origianl thread is here for reference:
https://www.experts-exchange.com/questions/26839982/re-work-an-ISC-DHCPD-CONF-file.html

Thanks
#!/usr/bin/perl

use strict;

my $block_level = 0;
my $in_group_scope = 0;
my $group_scope_block_level = 0;
my $group_option_domain;
while (<>) {
        next if (/^\s*failover peer/);
        if (/^\s*group\s*{/) {
                $in_group_scope = 1;
                $group_scope_block_level = $block_level;
                $block_level++;
                print "#$_";
                next;
        }
        if ($in_group_scope && /(option\s+domain-name\s+".*";)/) {
                $group_option_domain = $1;
                print "#$_";
                next;
        }
        if (/{\s*$/) {
                $block_level++;
        } elsif (/^\s*}/) {
                $block_level--;
                if ($in_group_scope && $group_scope_block_level == $block_level) {
                        $in_group_scope = 0;
                        $group_scope_block_level = 0;
                        $group_option_domain = '';
                        print "#$_";
                        next;
                }
        }
        print $_;
        if ($in_group_scope && /pool/) {
                print "  " x $block_level . $group_option_domain . "\n";
        }
}

Open in new window

bc-raw.txt
It seems to be working for me.
How do you run your script?
It does, but I need to remove this from the output


        }

    }
}

# DHCP_GROUP: SEC
{

and leave only the # DHCP_GROUP: SEC in each section
So you don't need to "balance out" the curly braces, i.e. this

# DHCP_GROUP: SEC
group
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;
        }

    }
}


Should become this:

group
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;


If not, please provide an example.

Thanks.
Yes. Correct.

Can the attached script be modified to do this. I could not figure it out.
Replace line 10 from
        next if (/^\s*failover peer/);
to
        next if (/^(\s*failover peer|# DHCP_GROUP|\s*})/);

and hopefully that will do the trick...

Good luck!
Ok. Let me try in the morning and let you know .

Thanks
Hi, not good yet.

Below is what it looks like from origianl script

dhcpd.conf

# DHCP_GROUP: GV
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    option ntp-servers 144.90.3.1;
    # DHCP_SUBNET: GV-ADM1
    subnet 144.90.212.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.212.1;
        # DHCP_POOL: GV-VLAN20
        pool
        {

            range 144.90.212.56 144.90.212.240;
        }

    }
    # DHCP_SUBNET: GV-ACA
    subnet 144.90.213.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 86400;
        ddns-domainname "edu-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.213.1;
        # DHCP_POOL: GV-VLAN30
        pool
        {

            range 144.90.213.56 144.90.213.240;
        }

    }
}

# DHCP_GROUP: SEC
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;
        }

    }
}

# DHCP_GROUP: PCAE-ES
{
    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: ES-ADM1
    subnet 144.90.232.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ddns-domainname "pcc-domain.pima.edu";
        authoritative ;
        option routers 144.90.232.1;
        # DHCP_POOL: VLAN20
        pool
        {

            option routers 144.90.232.1;
            range 144.90.232.56 144.90.232.240;
        }

    }

Open in new window


This is what I need it to look like

#dhcpd.conf


# DHCP_GROUP: GV

    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    option ntp-servers 144.90.3.1;
    # DHCP_SUBNET: GV-ADM1
    subnet 144.90.212.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.212.1;
        # DHCP_POOL: GV-VLAN20
        pool
        {

            range 144.90.212.56 144.90.212.240;
        }

    }
    # DHCP_SUBNET: GV-ACA
    subnet 144.90.213.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ping-check true;
        option netbios-node-type 8;
        default-lease-time 86400;
        ddns-domainname "edu-domain.pima.edu";
        max-lease-time 864000;
        option routers 144.90.213.1;
        # DHCP_POOL: GV-VLAN30
        pool
        {

            range 144.90.213.56 144.90.213.240;
        }

    }

# DHCP_GROUP: SEC

    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    ddns-domainname "pcc-domain.pima.edu";
    max-lease-time 604800;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: SE-ADM1
    subnet 144.90.221.128 netmask 255.255.255.128
    {
        ddns-updates true;
        option netbios-node-type 8;
        default-lease-time 604800;
        ddns-domainname "pcc-domain.pima.edu";
        max-lease-time 604800;
        option routers 144.90.221.129;
        # DHCP_POOL: SE-VLAN20
        pool
        {

            option routers 144.90.221.129;
            range 144.90.221.141 144.90.221.240;
        }

    }


# DHCP_GROUP: PCAE-ES

    option bootfile-name "BStrap\\X86pc\\BStrap.0";
    ddns-updates true;
    ping-check true;
    option netbios-node-type 8;
    next-server 144.90.6.5;
    option tftp-server-name "144.90.6.5";
    min-lease-time 86400;
    default-lease-time 604800;
    max-lease-time 864000;
    option domain-name-servers 144.90.3.119, 144.90.182.19;
    # DHCP_SUBNET: ES-ADM1
    subnet 144.90.232.0 netmask 255.255.255.0
    {
        ddns-updates true;
        ddns-domainname "pcc-domain.pima.edu";
        authoritative ;
        option routers 144.90.232.1;
        # DHCP_POOL: VLAN20
        pool
        {

            option routers 144.90.232.1;
            range 144.90.232.56 144.90.232.240;
        }

    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jeromee
jeromee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
AWESOME !
Glad you like.
Happy Perl'ing!