Solved

grep help

Posted on 2011-03-02
15
337 Views
Last Modified: 2012-05-11
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
Comment
Question by:richsark
  • 8
  • 7
15 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35022332
*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
 
LVL 1

Author Comment

by:richsark
ID: 35022523
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
 
LVL 1

Author Comment

by:richsark
ID: 35022653
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35022799
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
 
LVL 1

Author Comment

by:richsark
ID: 35023743
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35023930
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
 
LVL 1

Author Comment

by:richsark
ID: 35023961
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 26

Expert Comment

by:wilcoxon
ID: 35023982
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
 
LVL 1

Author Comment

by:richsark
ID: 35024089
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35024245
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
 
LVL 1

Author Comment

by:richsark
ID: 35024335
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35024534
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
 
LVL 1

Author Comment

by:richsark
ID: 35027465
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
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 35028708
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
 
LVL 1

Author Closing Comment

by:richsark
ID: 35028873
Thanks for baring with me and your kind help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now