Solved

script parse on values issue

Posted on 2011-03-03
13
282 Views
Last Modified: 2012-05-11
HI Guys,

I must be a dunce :)

I am trying to modify a script from a previous thread that Wilcoxon created.

The match I am trying to get are:

zone "samaritan.local" in {
        type slave;
        file "sec_qi/db.samaritan.local";
        masters { 10.100.20.175; 10.100.20.180; };
        allow-query { Any; };
        allow-transfer { "blesstransfers"; };
        notify no;
};

zone "_msdcs.samaritan.local" in {
        type slave;
        file "sec_qi/db._msdcs.samari.local";
        masters { 10.100.20.175; 10.100.20.180;};
        check-names ignore;
        allow-query { any; };
        allow-transfer { 128.163.97.5;128.163.3.10;128.163.1.6; };
        notify no;
};

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

zone samaritan.local allow-transfer="[blesstransfers]"
zone _msdcs.samaritan.local allow-transfer="[128.163.97.5;128.163.3.10;128.163.1.6]"

Code is on next thread..

0
Comment
Question by:richsark
13 Comments
 
LVL 1

Author Comment

by:richsark
ID: 35032464
Here is the code I was messing with. But I am missing something
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update,$allow-transfer $);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone,$allow_update,$allow-transfer) {
            $allow-update =~ s{;\s*$}{};
            $allow-transfer =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update $allow-transfer]\"\n"
        }
        $zone = "zone $1";
        $allow-update = '';
        $allow-transfer = '';
    }
    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,$allow_update,$allow-transfer ) {
    $allow_update =~ s{;\s*$}{};
    print "$zone allow_update=\"[$allow_update]\"\n[$allow_transfer]\"\n"
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35033043
Just a gingal ..
0
 
LVL 1

Author Comment

by:richsark
ID: 35033215
This is what I am seeing

$ perl chomp.pl ncc.conf.original

Can't use global $) in "my" at chomp.pl line 6, near "transfer $)"
syntax error at chomp.pl line 6, near "$);"
Global symbol "$allow_transfer" requires explicit package name at chomp.pl line 30.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 11.
Bareword "update" not allowed while "strict subs" in use at chomp.pl line 12.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 13.
Bareword "update" not allowed while "strict subs" in use at chomp.pl line 17.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 18.
Bareword "transfer" not allowed while "strict subs" in use at chomp.pl line 28.
Execution of chomp.pl aborted due to compilation errors.
0
 
LVL 1

Author Comment

by:richsark
ID: 35033432
Hi Guys,

I am trying to solve this, but again I am not doing it right.

Please help
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update $allow-transfer $allow_transfer $);

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update $allow_transfer) {
            $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 Comment

by:richsark
ID: 35035821
Hi, did I do something wrong that I yet to get help on ?

Rich
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35036006
Does it have to be Perl? How about awk, tr, sed?

#!/bin/sh
inputfile=/path/to/inputfile
awk  '/^zone/           {printf $1 " " $2 " "};
      /allow-transfer/  {printf $1 "=#[" $2 $3 "]#\n"}' $inputfile \
      | tr -d "{\"" \
      | tr "#" "\"" \
      | sed "s/;]/]/g"
exit

Open in new window


wmp
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:richsark
ID: 35036291
sure, that would be fine,

I ran it, looks good, but only the first line is appended with a few zones

zone ikuu8z.com zone ikuu8q.com zone molendf.kr zone cs.engr.uky.edu allow-transfer="[blesstransfers]"
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35036329
Seems that your sample is not representative for what you're actually going to process!

Obviously there are "zone" entries without a following "allow-transfer"!
0
 
LVL 1

Author Comment

by:richsark
ID: 35036803
right, I do not want it to report if there are no allow-transfer filled in.

I am also trying to get allow-update stuff too. so if its filled in as "None" for either, then dont report it.

Does that make sense?
0
 
LVL 1

Author Comment

by:richsark
ID: 35037815
HI,

all I need is to have an output like so for both allow_transfer and allow_update


zone modify _msdcs.as.foo.edu set allow_transfer="[128.163/16]"
zone modify _msdcs.as.foo.edu set allow_update="[128.163.119.96,128.163.119.95,128.163.119.94,128.163.119.93;128.163.119.103]"

I tried playing with it, but I cant get it. please help
#!/bin/sh
inputfile=ncc.conf.original
awk  '/^zone/           {printf $1 " " $2 " "};
      /allow-transfer/  {printf $1 "=#[" $2 $3 "]#\n"}' $inputfile \
      /allow-update/  {printf $1 "=#[" $2 $3 "]#\n"}' $inputfile \
      | tr -d "{\"" \
      | tr "#" "\"" \
      | sed "s/;]/]/g"
exit

Open in new window

0
 
LVL 22

Expert Comment

by:blu
ID: 35038779
Back to your error messages. The problem you are showing is that you used dashes "-" instead of underscores "_" in the variable names.
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 35038884
This should do what you want...

Again, part of your problem is that $allow-transfer is not a legal variable name ($allow_transfer is - underscore is legal but dash is not).
#!/usr/bin/perl

use strict;
use warnings;

my ($zone, $allow_update, $allow_xfer);

while (<DATA>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $allow_update =~ s{;\s*$}{};
            print "$zone allow_update=\"[$allow_update]\"\n"
        }
        if ($zone and $allow_xfer) {
            $allow_xfer =~ s{;\s*$}{};
            $allow_xfer =~ s{;\s*}{, }g;
            print "$zone allow_transfer=\"[$allow_xfer]\"\n";
        }
        $zone = "zone $1";
        $allow_update = '';
        $allow_xfer = '';
    }
    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 (/^\s*allow-transfer\s*{\s*\S+/../}/) {
        if (/^\s*allow-transfer\s*{\s*(\S+)/) {
            $allow_xfer = $1;
            $allow_xfer =~ s{"}{}g;
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*$}{};
    print "$zone allow_update=\"[$allow_update]\"\n";
}
if ($zone and $allow_xfer) {
    $allow_xfer =~ s{;\s*$}{};
    $allow_xfer =~ s{;\s*}{, }g;
    print "$zone allow_transfer=\"[$allow_xfer]\"\n";
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 35039107
Oh... Thanks wilcoxon, Let me look at your code to understand. Thank you kindly

Rich
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now