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
PerlShell Scripting

Avatar of undefined
Last Comment
jeromee
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
Avatar of jeromee
jeromee
Flag of United States of America image

It seems to be working for me.
How do you run your script?
Avatar of richsark
richsark
Flag of United States of America image

ASKER

It does, but I need to remove this from the output


        }

    }
}

# DHCP_GROUP: SEC
{

and leave only the # DHCP_GROUP: SEC in each section
Avatar of jeromee
jeromee
Flag of United States of America image

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.
Avatar of richsark
richsark
Flag of United States of America image

ASKER

Yes. Correct.

Can the attached script be modified to do this. I could not figure it out.
Avatar of jeromee
jeromee
Flag of United States of America image

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!
Avatar of richsark
richsark
Flag of United States of America image

ASKER

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

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

ASKER

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of richsark
richsark
Flag of United States of America image

ASKER

AWESOME !
Avatar of jeromee
jeromee
Flag of United States of America image

Glad you like.
Happy Perl'ing!
Perl
Perl

Perl is a high-level, general-purpose, interpreted, dynamic programming languages with over 25 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large-scale development projects. Perl gained widespread popularity as a Common Gateway Interface (CGI) scripting language, in part due to its regular expression and string parsing abilities. In addition to CGI, Perl is used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications.

26K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo