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

asked on

script not finding entire string

Hi guys,

I am working with a perl script that I feed to a file. Then it creates an output

I noticed that values that have multiple IP address dont get captured.

In the master file I have:

};

zone "_msdcs.cvve.foo.edu" in {
      type master;
      file "db.msdcs.cvve.foo.edu";
      allow-update { 128.163.234.17; };
      allow-query { Any; };
      allow-transfer { 128.163.1.6; 128.163.3.10; 128.163.234.17; };
      notify no;
      check-names Ignore;
};

The output I get;

zone _msdcs.cvve.foo.edu allow_update="[128.163.234.17]"
zone _msdcs.cvve.foo.edu allow_transfer="[128.163.1.6]"

as you can see, allow_transfer should of looked like this:

zone _msdcs.cvve.foo.edu allow_transfer="[128.163.1.6,128.163.3.10,128.163.234.17]"

I will post the script on the next thread.

Thanks in advance for you help !

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

ASKER

See code below:

I cant seem to understand why for allow_transfer is only showing 1 IP address on some??

Is it because one that are listed as /16 is screwing up the output?

};

zone "_msdcs.as.foo.edu" in {
      type master;
      file "db._msdcs.as.foo.edu";
      allow-update { 128.163.119.96;128.163.119.95;128.163.119.94;128.163.119.93;128.163.119.103; };
      allow-query { Any; };
      allow-transfer { 128.163/16; };
      notify no;
};

Thanks.
#!/usr/bin/perl

use strict;
use warnings;

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

while (<>) {
    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

Avatar of wilcoxon
Generally, it would be something like this to capture the addresses:

$line =~ m{allow-transfer\s*{\s*(\d[\d\.;]+?);\s*};
$xfer_addr = $1;

I'll look at your script when it is posted...
Hi wilcoxon,

Script posted.
This should fix it...
#!/usr/bin/perl

use strict;
use warnings;

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

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $allow_update =~ s{;\s*$}{};
            $allow_update =~ s{;\s*}{, }g;
            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[\/\w\d\.;\s]+?);\s*}/) {
            $allow_xfer = $1;
            $allow_xfer =~ s{"}{}g;
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*$}{};
    $allow_update =~ s{;\s*}{, }g;
    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

Yup, That did it

I see you modified line 33, is that right?

if (/^\s*allow-transfer\s*{\s*(\S[\/\w\d\.;\s]+?);\s*}/) {
Yep.  That was the important change.  The only other changes I made was to make output spacing a little nicer (adding lines 13 and 42 - similar to what was already there for $allow_xfer).
Oh... Some lines have text in them for allow transfer and allow update, I need that as well.

Then I can bring to xcel and do a find replace on the name and convert to IP's

};

zone "worldof.org" in {
      type master;
      file "db.worldof.org";
      allow-update { None; };
      allow-query { Any; };
      allow-transfer { "blesstransfers"; };
      notify no;
};
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
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
SOLUTION
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
That looks fine to me.
Sorry to be a pain

It should be like so, in addition, I played with the script to look like so...

conf zone modify foo.com set allow_transfer="[168.147.0.0/24,192.168.114.0/24]"

but I am getting

conf zone foo.com modify set allow_transfer="[168.147.0.0/24,192.168.114.0/24]"

Whay am i missing?
#!/usr/bin/perl

use strict;
use warnings;

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

while (<>) {
    chomp;
    if (/^\s*zone\s*"([^"]+)"/) {
        if ($zone and $allow_update) {
            $allow_update =~ s{;\s*$}{};
            $allow_update =~ s{;\s*}{, }g;
            print "conf $zone modify set allow_update=\"[$allow_update]\"\n"
        }
        if ($zone and $allow_xfer) {
            $allow_xfer =~ s{;\s*$}{};
            $allow_xfer =~ s{;\s*}{, }g;
            print "conf $zone modify set 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["\/\w\d\.;\s]+?);\s*}/) {
            $allow_xfer = $1;
            $allow_xfer =~ s{"}{}g;
        }
    }
}
if ($zone and $allow_update) {
    $allow_update =~ s{;\s*$}{};
    $allow_update =~ s{;\s*}{, }g;
    print "conf $zone modify set allow_update=\"[$allow_update]\"\n";
}
if ($zone and $allow_xfer) {
    $allow_xfer =~ s{;\s*$}{};
    $allow_xfer =~ s{;\s*}{, }g;
    print "conf $zone modify set allow_transfer=\"[$allow_xfer]\"\n";
}

Open in new window

I can open a new thread if I am out of line???
Sorry, made a mistake. I did not want to close this. I wanted to award points
If I'm understanding what you're asking, You can simply change your print statements.  This would be for the allow_update (line 14 and 43):

print "conf zone modify $zone set allow_update=\"[$allow_update]\"\n";
Thanks for the quick response