• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

grep help

Hi guys,

I am trying to grep out certain items from a named.conf

};

zone "3.5.10.in-addr.arpa" {
      type master;
      file "db.10.5.3";
      allow-update { w2kdcs;
                  key dhcp-dns.foo.edu.;
                  10.8.1.231; };
};

zone "10.6.10.in-addr.arpa" {
      type master;
      file "db.10.6.10";
      allow-update { 10.8.1.231;
                  key dhcp-dns.findlay.edu.; };
};

When I run the command, it does not look right:

$ perl chomp-named.pl named.conf
-update-update-update-update-update-update-update-update-update-updatezone "0.0.127.in-addr.arpa"
-updatezone "1.10.in-addr.arpa"
-updatezone "1.10.in-addr.arpa"
-updatezone "2.10.in-addr.arpa"
-updatezone "2.10.in-addr.arpa"
-updatezone "1.3.10.in-addr.arpa"
-updatezone "1.3.10.in-addr.arpa"

I am trying to get an output like so

zone 3.5.10.in-addr.arpa allow_update="[w2kdcs]"
zone 10.6.10.in-addr.arpa allow_update="[10.8.1.231]"

Thank you



while (<>) {
        if (/zone.*{/) {
                $zone = $_;
                $zone =~ s/\{//;

        }
        if (/^.allow-update.*/) {
#              $allow-update = $_;
#                $allow-update =~ s/\{//;

        }
                if (/^.*}/) {
                print "$zone$allow-update";
        }
}

Open in new window

0
richsark
Asked:
richsark
  • 8
  • 7
1 Solution
 
wilcoxonCommented:
*ALWAYS* use strict and warnings.  It will help you catch a lot of errors.

Part of the problem is that $allow-update is an illegal variable name.

This will work...
#!/usr/local/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update" if ($zone and $allow_update);
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update" if ($zone and $allow_update);

Open in new window

0
 
richsarkAuthor Commented:
Thanks wilcoxon!

I ran it.

I created a file called chomp.pl

executed:

perl chomp.pl named.conf > output.txt

But its all over the place.

zone 1.10.in-addr.arpa allow_update="[10]"zone 2.10.in-addr.arpa allow_update="[10]"zone 1.3.10.in-addr.arpa allow_update="[w2kdcs]"zone 2.3.10.in-addr.arpa allo

I would like it one per line in this format please.

zone 3.5.10.in-addr.arpa allow_update="[w2kdcs]"
zone 10.6.10.in-addr.arpa allow_update="[10.8.1.231]"
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update" if ($zone and $
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update" if ($zone and $allow_update);

Open in new window

0
 
richsarkAuthor Commented:
HI,

I tried the \n and did not work


#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update" if ($zone and $allow_update);
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update" if ($zone and $allow_update);"\n"

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
wilcoxonCommented:
This should fix it.
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update\n" if ($zone and $
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update\n" if ($zone and $allow_update);

Open in new window

0
 
richsarkAuthor Commented:
Hi wilcoxon:

Sorry to trouble you, but the above code is giving me an error on line 12

$ perl chomp-named.pl named.conf

syntax error at chomp-named.pl line 12, near ""zone $1";"
Execution of chomp-named.pl aborted due to compilation errors.
0
 
wilcoxonCommented:
Sorry.  Somehow, a partial line got truncated (no idea how it lost just a part of one line).
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update\n" if ($zone and $allow_update);
                $zone = "zone $1";

        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update\n" if ($zone and $allow_update);

Open in new window

0
 
richsarkAuthor Commented:
Hi,

Much better, but on some I am seeing on a zpne section:

zone "foo.edu" {
        type master;
        file "db.foo.edu";

#        allow-update { w2kdcs;
#                        w2kservers;
#                        key dhcp-dns.foo.edu.;
#                        10.8.1.231; };

Output is showing

zone foo.edu allow_update="[10]"
0
 
wilcoxonCommented:
Ah.  That's because everything is done with global variables.  I best 10 is the allow-update for the previous zone.  If there is no allow-update what do you want to output (if anything)?

This will fix it so that the incorrect allow_update will not be output but the condition currently only outputs a zone if allow_update is populated (not sure if that's what you want or not).
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
        chomp;
        if (/^\s*zone\s*"([^"]+)"/) {
                print "$zone $allow_update\n" if ($zone and $allow_update);
                $zone = "zone $1";
                undef $allow_update;
        }
        if (/^\s*allow-update\s*{\s*(\w+)/) {
              $allow_update = "allow_update=\"[$1]\"";

        }
}
print "$zone $allow_update\n" if ($zone and $allow_update);

Open in new window

0
 
richsarkAuthor Commented:
HI,

OK,
This is what I get now for some sections like so:

zone "1.10.in-addr.arpa" {
        type master;
        file "db.10.1";
        allow-update { 10.8.1.231; };
};

zone "2.10.in-addr.arpa" {
        type master;
        file "db.10.2";
        allow-update { 10.8.1.231; };
};

Output is:

zone 1.10.in-addr.arpa allow_update="[10]"
zone 2.10.in-addr.arpa allow_update="[10]"

Looks like we need to compensate for IP's as well as names

If there is no allow-update set for a zone, then I would not to report on it. Does that make sense?

Stuff like this

zone "4.5.10.in-addr.arpa" {
        type master;
        file "db.10.5.4";
        allow-update { w2kdcs;
                        key dhcp-dns.foo.edu.;
                        10.8.1.231; };
};

I would need the w2kdcs; and 10.8.1.231 and not the key dhcp-dns.findlay.edu. part please


Thanks a $$$$
0
 
wilcoxonCommented:
This should do it...
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $allow_update =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update]\"\n"
        }
        $zone = "zone $1";
        $allow_update = '';
    }
    if (/^\s*allow-update\s*{\s*\w+/.../}/) {
        if (/^\s*allow-update\s*{\s*(\S+)/) {
            $allow_update = $1;
        } elsif (/(\d+\.\d+\.\d+\.\d+)/) {
            $allow_update .= " $1";
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*}{};
    print "$zone allow_update=\"[$allow_update]\"\n";
}

Open in new window

0
 
richsarkAuthor Commented:
HI wilcoxon, Awesome, but :)...I noticed this...

for parts like these, I need to capture both lines

};

zone "1.8.10.in-addr.arpa" {
        type master;
        file "db.10.8.1";
        allow-update { w2kdcs;
                        w2kservers;
                        10.8.1.231;
                        key dhcp-dns.foo.edu.; };
};

Like so:
zone 1.8.10.in-addr.arpa allow_update="[w2kdcs;w2kservers;10.8.1.231]"

Thanks again !
0
 
wilcoxonCommented:
This should fix it...
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $allow_update =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update]\"\n"
        }
        $zone = "zone $1";
        $allow_update = '';
    }
    if (/^\s*allow-update\s*{\s*\w+/../}/) {
        if (/^\s*allow-update\s*{\s*(\S+)/) {
            $allow_update = $1;
        } elsif (not /\bkey\b/ and /^\s*([^;]+;)/) {
            $allow_update .= " $1";
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*$}{};
    print "$zone allow_update=\"[$allow_update]\"\n";
}

Open in new window

0
 
richsarkAuthor Commented:
Cool, One slight observation I see

for sections:

zone "5.252.10.in-addr.arpa" {
        type master;
        file "db.10.252.5";
        allow-update { key dhcp-dns.foo.edu.;
                        10.8.1.231; };
};

zone "6.252.10.in-addr.arpa" {
        type master;
        file "db.10.252.6";
        allow-update { key dhcp-dns.foo.edu.;
                        10.8.1.231; };
};

output looks like:

zone 5.252.10.in-addr.arpa allow_update="[key 10.8.1.231]"
zone 6.252.10.in-addr.arpa allow_update="[key 10.8.1.231]"

Thanks again for all your efforts
0
 
wilcoxonCommented:
This should fix this problem.

At this point, it seems like you should be able to modify the script further to fix any other issues based on the multitude of versions I've posted above.
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $allow_update =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update]\"\n"
        }
        $zone = "zone $1";
        $allow_update = '';
    }
    if (/^\s*allow-update\s*{\s*\w+/../}/) {
        if (/^\s*allow-update\s*{\s*(\S+)/) {
            $allow_update = $1;
            $allow_update = '' if ($allow_update eq 'key');
        } elsif (not /\bkey\b/ and /^\s*([^;]+;)/) {
            $allow_update .= ($allow_update ? ' ' : '') . $1;
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*$}{};
    print "$zone allow_update=\"[$allow_update]\"\n";
}

Open in new window

0
 
richsarkAuthor Commented:
Thanks for baring with me and your kind help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now