Extracting data from zone files

I'm looking for a solution to the following problem...
I need a 3 column file of the domains like this
mydomain1.tld 10.0.0.85 ns1.mywebserver.tld.
mydomain2.tld 10.0.0.85 ns1.mywebserver.tld.
mydomain3.tld 10.0.0.86 ns1.myotherserver.tld.

I'm  in  the named zone root directory & grep returns a list of zone files like this..
#grep -lr 'SOA' *
zones5/12/experts-exchange.com
zones4/11/mydomain1.tld
zones4/15/mydomain2.tld

I can get the data into individual lists but not combined...
eg the domain names using..
# grep -r 'SOA' *|grep 'zones'|cut -d ':' -f 1|cut -d '/' -f 3
the dns server..
# cat `grep -r 'SOA' *|grep 'zones'|cut -d ':' -f 1`|grep '*'|awk '{print $5}
the IP of the website
cat `grep -r 'SOA' *|grep 'zones'|cut -d ':' -f 1`|grep 'SOA'|awk '{print $4}'

Here is a sample zone file
# cat /hsphere/local/var/named/zones16/5/maverick.tld
;BIND DUMP V8
$ORIGIN tld.
maverick   86400   IN      NS      ns2.myserver.tld.    ;Cl=3
        86400   IN      NS      ns1.myserver.tld.    ;Cl=3
        86400   IN      SOA     ns2.myserver.tld. nocadmin.myserver.tld. (
                2003123101 10800 3600 604800 86400 )    ;Cl=3
        86400   IN      A       10.0.0.84  ;Cl=3
        86400   IN      MX      10 pop3.maverick.tld.        ;Cl=3
$ORIGIN maverick.tld.
mail    86400   IN      CNAME  10  maverick.tld.   ;Cl=3
*       86400   IN      A       10.0.0.84  ;Cl=3

Thanks everyone
MAVERICK
LVL 1
MAVERICKAsked:
Who is Participating?
 
ahoffmannConnect With a Mentor Commented:
awk 'BEGIN{ns="@";d="@"}($3=="SOA"){ns=$4;next}($1~"ORIGIN"&&ns!="@"){d=$2;next}($4=="A"){print d" "$5" "ns}' zone15/5/discountersplus.com

# to be improved in many ways, for example removing the trailing dot
0
 
gheistCommented:
What addresses you need in that file ???
0
 
MAVERICKAuthor Commented:
I need the 3 columns

If you mean IP Address - Assume first A record of the file.
 86400   IN      A       10.0.0.84  ;Cl=3

The hostname from the zone filename is fine.

Thanks
-John
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
tfewsterCommented:
man paste ;-)
0
 
MAVERICKAuthor Commented:
Close but no cigar ;-)

The main problem is that there is a chance the IPs/DNS wont line up with the hostnames

I tried doing it manually in excel and it didn't work out.

Thanks alot
Maverick
0
 
tfewsterConnect With a Mentor Commented:
Or if you don't like that:

#!/usr/bin/ksh
# zone.ksh  - Or whatever you call it, remember to exclude it from the list...

for ZONE in `grep -r 'SOA' * | grep -v zone.ksh|grep 'zones'|cut -d ':' -f 1`
do
  DNS=`echo $ZONE |cut -d '/' -f 3 `            # Or DNS=`basename $ZONE`
  IP=`grep '*' $ZONE|awk '{print $5}'`
  DOMAIN=`grep 'SOA' $ZONE|awk '{print $4}'`
  echo $DNS $IP $DOMAIN
done
0
 
ahoffmannCommented:
your examples do not match
could you please give a zone file and a example using this zone file to demonstrate what you expect as result

Also zone files can have a lot of different formats, with line breaks etc.
If you can guarantee that you onaly have *one* fixed format awk would do it, otherwise (several formats) start with doing it in perl
0
 
TintinCommented:
Running any of your greps over the sample zone file you supplied will return nothing.  What is the actual data you're working with.
0
 
MAVERICKAuthor Commented:
tfewster: What is ksh?
ahoffmann: they are h-sphere autogenerated zone files - consistently the same

This is one of the actual zone files from the server;
[root@cp named]# cat zones15/5/discountersplus.com
;BIND DUMP V8
$ORIGIN com.
discountersplus 86400   IN      NS      ns4.ddwarehouse.com.    ;Cl=2
        86400   IN      NS      ns3.ddwarehouse.com.    ;Cl=2
        86400   IN      SOA     ns4.ddwarehouse.com. hostmaster.change-me-asap.com. (
                2003123101 10800 3600 604800 86400 )    ;Cl=2
        86400   IN      A       216.240.151.84  ;Cl=2
        86400   IN      MX      10 pop3.ddwarehouse.com.        ;Cl=2
$ORIGIN discountersplus.com.
mail    86400   IN      CNAME   pop3.ddwarehouse.com.   ;Cl=2
*       86400   IN      A       216.240.151.84  ;Cl=2

The zone15/5/discountersplus.com

The expected result in the output should be;
discountersplus.com 216.240.151.84 ns4.ddwarehouse.com.

Thanks alot
Maverick
0
 
tfewsterCommented:
Ksh=Korn shell, but that simple script should work under Bourne shell (sh) as well. It handles the case of e.g multiple SOA lines (tho' I have no idea if that's valid in a zone file) but line breaks could confuse it and you'd get a blank field in the output.

Anyway, give it a try and see what happens.
0
 
TintinCommented:
If the zone file is exactly as you have shown, it is impossible that any of your grep statements would have returned anything.  You've either mistyped the commands you are running, or you are using different data
0
 
MAVERICKAuthor Commented:
tfewster: your solution basically worked as intended.

The zone files do vary in structure slightly - Domains with subdomains were not processed correctly. The solution was to hand edit those in excel.

Thanks alot.
0
 
gheistCommented:
Do you have excel for UNIX?
Cool!!!
0
 
MAVERICKAuthor Commented:
I copied the file onto my windows desktop PC for touchup and formatting.

I'm not sure how well the WINE libraries run under Unix, like such as FreeBSD. I'm curious that they make Excel for OS X, which is FreeBSD based.

:)
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.

All Courses

From novice to tech pro — start learning today.