in reply to Bind zone file search
Perhaps the following will offer some direction:
use strict; use warnings; my $directory = './zones'; my %zones = getZoneHash($directory); print "$_ -> $zones{$_}\n" for keys %zones; sub getZoneHash { my ($dir) = @_; my %hash; local $/; for my $file ( grep -f, <"$dir/*"> ) { open my $fh, '<', $file or die $!; my $data = <$fh>; close $fh; $hash{$1} = $2 if $data =~ /zone\s+"([^"]+)".+masters\s+{\s*([^}\s]+)\s*}/s +; } Kg return %hash; }
There are three files in the directory zones, and each contains only one of the following zone stanzas:
zone "zone.com" { type slave; file "path/to/db.zone"; masters {ip.add.ress.here}; }; zone "cnn.com" { type slave; file "path/to/db.zone"; masters { cnn.add.ress.here }; }; zone "perlmonks.org" { type slave; file "path/to/db.zone"; masters { ip.perlmonks.ress.here }; };
Output:
cnn.com -> cnn.add.ress.here perlmonks.org -> ip.perlmonks.ress.here zone.com -> ip.add.ress.here
The subroutine getZoneHash takes a directory parameter, and then opens, reads, and captures the zone info and masters ip address of the zone stanza in each file in that directory, building and returning a hash.
The capturing regex:
/zone\s+"([^"]+)".+masters\s+{\s*([^}\s]+)\s*}/s ^ ^ ^ ^ ^ ^ ^ ^ ^ | | | | | | | | | | | | | | | | | + - Treat string as sin +gle line | | | | | | | + - 0+ whitespaces | | | | | | + - Match anything that's not } o +r a whitespace | | | | | + - 0+ whitespaces | | | | + - 1+ whitespaces | | | + - End quotation | | + - Match anything that's not a " | + - Begin quotation + - 1+ whitespaces
Hope this helps!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Bind zone file search
by ranceh (Novice) on Sep 26, 2012 at 13:27 UTC | |
by Kenosis (Priest) on Sep 26, 2012 at 16:22 UTC | |
Re^2: Bind zone file search
by ranceh (Novice) on Sep 26, 2012 at 14:53 UTC | |
by Kenosis (Priest) on Sep 26, 2012 at 16:21 UTC | |
by ranceh (Novice) on Sep 26, 2012 at 16:54 UTC | |
by Kenosis (Priest) on Sep 26, 2012 at 16:58 UTC | |
by ranceh (Novice) on Sep 26, 2012 at 18:37 UTC | |
|
In Section
Seekers of Perl Wisdom