How to convert forward zone file to reverse


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert forward zone file to reverse
# 1  
Old 01-29-2009
How to convert forward zone file to reverse

I have forward dns zone file with A records and converting it to reverse zone file
ex.

subdomain1 A 10.20.30.40
subdomain2 A 10.20.31.41
subdomain3 A 10.20.35.43
subdomain4 A 10.20.30.41
subdomain5 A 10.20.30.42

Need to convert reverse like:

10.20.30.40
40 PTR subdomain1.domain.com
41 PTR subdomain4.domain.com
42 PTR subdomain5.domain.com
10.20.31.41
41 PTR subdomain2.domain.com
10.20.35.43
43 PTR subdomain3.domain.com

I am doing this with below command but not satisfied, I know there is better way to do this but need right direction.

Code:
for line in $(cat zonefile );
do
        name = $(echo $line | cut -d' ' -f1)
        IP = $(echo $line | cut -d. -f4)
        echo -e "$IP \t PTR \t $name.domanin.com"
done

# 2  
Old 01-29-2009
Quote:
Originally Posted by mosavani
I have forward dns zone file with A records and converting it to reverse zone file
ex.

subdomain1 A 10.20.30.40
subdomain2 A 10.20.31.41
subdomain3 A 10.20.35.43
subdomain4 A 10.20.30.41
subdomain5 A 10.20.30.42

Need to convert reverse like:

10.20.30.40
40 PTR subdomain1.domain.com
41 PTR subdomain4.domain.com
42 PTR subdomain5.domain.com
10.20.31.41
41 PTR subdomain2.domain.com
10.20.35.43
43 PTR subdomain3.domain.com

I am doing this with below command but not satisfied, I know there is better way to do this but need right direction.

I doubt that you are doing it with the script you posted; there are errors in that script. When posting code, use the exact code. Cut and paste it, do not retype it.
Quote:

Code:
for line in $(cat zonefile );


That is almost always the wrong way to read a file.

You are not reading it a line at a time, but a word at a time.

Use a while loop:

Code:
while IFS= read -r line
do
  : ...
done < zonefile

Quote:
Code:
do
        name = $(echo $line | cut -d' ' -f1)
        IP = $(echo $line | cut -d. -f4)


There must be no spaces around the equals signs.

You do not need an external command to split a line. Use the shell's parameter expansion or IFS and set:

Code:
name=${line%% *}
set -f; IFS=.; set -- $line
shift $(( $# - 1 ))
IP=$4

Quote:
Code:
        echo -e "$IP \t PTR \t $name.domanin.com"
done

# 3  
Old 01-29-2009
There's usually not much reason to use cut when the read builtin can handle delimiters more elegantly. Here it takes two delimiters, period and space, to split the string into words and individual numbers at the same time. It also checks when the first three change and prints out the address entire, as your output data suggests you want.

Code:
IFS=" ."
while read NAME G A B C D && [[ ! -z "$NAME" ]]
do
        if      [[ "$A" != "$AA" ]]     ||
                [[ "$B" != "$BB" ]]     ||
                [[ "$C" != "$CC" ]]
        then
                echo "$A.$B.$C.$D"
        fi

        AA="$A" ; BB="$B" ; CC="$C" ; DD="$D"

        echo -e "$D\tPTR\t$NAME.domain.com"
done < zonefile

# 4  
Old 01-30-2009
Quote:
Originally Posted by cfajohnson

I doubt that you are doing it with the script you posted; there are errors in that script. When posting code, use the exact code. Cut and paste it, do not retype it.
I was looking for right direction and you gave it me... thank you!

Quote:
Originally Posted by Corona688
There's usually not much reason to use cut when the read builtin can handle delimiters more elegantly. Here it takes two delimiters, period and space, to split the string into words and individual numbers at the same time. It also checks when the first three change and prints out the address entire, as your output data suggests you want.
you got it right! thank you!
# 5  
Old 01-30-2009
The output data doesn't look right to me because the
lines:

10.20.30.40
40 PTR subdomain1.domain.com
41 PTR subdomain4.domain.com
42 PTR subdomain5.domain.com

don't look like they imply that the last 40 is substituted by the first field in subsequent lines.

At any rate -- here's my ksh solution (late)

Code:
 
prev_key=some-junk
 
cat << EOF |
subdomain1 A 10.20.30.40
subdomain2 A 10.20.31.41
subdomain3 A 10.20.35.43
subdomain4 A 10.20.30.41
subdomain5 A 10.20.30.42
EOF
#----------------------------------------------------------------------#
# Convert spaces to dots.                                              #
#----------------------------------------------------------------------#
sed -e's/ /./g' |
#----------------------------------------------------------------------#
# Delimit on dots... reformat so that the key is in front...           #
#----------------------------------------------------------------------#
awk -F. '{
  printf( "%s.%s.%s %s %s\n", $3, $4, $5, $6, $1 );
  }' |
while read key subnet domain; do
 
  if [[ $key != $prev_key ]]; then
    print $key
    prev_key=$key
  fi
 
  print $subnet PTR $domain.domain.com
 
done

# 6  
Old 01-30-2009
If you have:

Code:
ip="192.168.1.2"

You can:

Code:
set `echo "$ip" | sed 's/\./ /g'`
revip=$4.$3.$2.$1.in-addr.arpa

But sheesh.. editing zone files?? Ever consider using dynamic updates to your BIND instead? A whole lot easier IMHO.


I know that's not exactly what you were looking for, but may take you there.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

2. Ubuntu

Iptables forward traffic to forward chain!!!

Hi, I am new to linux stuff. I want to use linux iptables to configure rule so that all my incoming traffic with protocol "tcp" is forwarded to the "FORWARD CHAIN". The traffic i am dealing with has destination addresss of my machine but i want to block it from coming to input chain and somehow... (0 Replies)
Discussion started by: arsipk
0 Replies

3. Solaris

Zone failes to boot due to mount issue, dir exists in zone.

I have two physical servers, with zones that mount local storage. We were using "raw device" in the zonecfg to point to a metadevice on the global zone (it was not mounted in the global zone at any point). It failed to mount on every boot because the directory existed in the zone. I... (6 Replies)
Discussion started by: BG_JrAdmin
6 Replies

4. Shell Programming and Scripting

Convert hex to decimal or reverse is better?

Please Help Me! about the problem down under. I have 2 files with nearly the same characteristics, I have to convert one to the other format or the other format to one's format. I want to write it with awk. The first file contain lines like this: 300000001#A#Y#Y#Y#Y The other file contain... (4 Replies)
Discussion started by: Axel82
4 Replies

5. UNIX for Dummies Questions & Answers

DNS forward and reverse entry required thorugh scripting instead of manually done

HI GUrus, I am very mcuh new for this forum ....i need you all guys help for resolving my queries. As i have DNS server , platform is sunsolaris----8. We are using that serevr for resolving all network nodes. I am going to explain the process what i am following .... I could have... (2 Replies)
Discussion started by: leenagoyal
2 Replies

6. UNIX for Advanced & Expert Users

Forward/Reverse Hostname Resolution for Oracle Install

I've got an old v880 running Sol10 that is going to another location. When the system moves, the domainname in resolv.conf will change also. My DBA's are going through the steps of installing Oracle on the box and they want the install program to resolve the hostname is if the box were already in... (2 Replies)
Discussion started by: bluescreen
2 Replies

7. Solaris

Bind zone transfer recategorises zone file?

Hi, I have a primary/slave Bind 9 setup running on a Solaris 10 platform. Everything is hunky dorey, except for when I make a zone file change and up the serial the file that is transferred to the slave looses all its comments, and the entries are place in alphabetical order. I administer many... (1 Reply)
Discussion started by: fulhamfcboy
1 Replies

8. Shell Programming and Scripting

delete and move number forward..in file

I have a file, can I just modify the <number> field and delete the 3rd number and move 4th number to 3rd.. (surely, delete the last digit)? <john> <Number>11 20 03 22 23 21 91 00 F0</Number> <peter> <Number>12 20 03 20 99 21 91 20 F0</Number> <ken> <id> <Number>10 21 03 21 78 21 92... (6 Replies)
Discussion started by: happyv
6 Replies

9. UNIX for Dummies Questions & Answers

.forward file for mail redirection

I've set up a .forward file on a few identical servers that we have (all identical .forward file as well) but only one server is forwarding any of my unix mail (mail generated by cron) to my internet account. Because these servers (5) are all identical in terms of aix versions, etc., I do not... (3 Replies)
Discussion started by: giannicello
3 Replies
Login or Register to Ask a Question