Solved

Extracting data from zone files

Posted on 2004-08-09
14
537 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:MAVERICK
  • 5
  • 3
  • 2
  • +2
14 Comments
 
LVL 61

Expert Comment

by:gheist
Comment Utility
What addresses you need in that file ???
0
 
LVL 1

Author Comment

by:MAVERICK
Comment Utility
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
 
LVL 20

Expert Comment

by:tfewster
Comment Utility
man paste ;-)
0
 
LVL 1

Author Comment

by:MAVERICK
Comment Utility
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
 
LVL 20

Assisted Solution

by:tfewster
tfewster earned 100 total points
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:MAVERICK
Comment Utility
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
 
LVL 20

Expert Comment

by:tfewster
Comment Utility
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
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
Comment Utility
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
 
LVL 1

Author Comment

by:MAVERICK
Comment Utility
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
 
LVL 61

Expert Comment

by:gheist
Comment Utility
Do you have excel for UNIX?
Cool!!!
0
 
LVL 1

Author Comment

by:MAVERICK
Comment Utility
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

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 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

12 Experts available now in Live!

Get 1:1 Help Now