LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2009, 09:37 PM   #1
KinnowGrower
Member
 
Registered: May 2008
Location: Toronto
Distribution: Centos && Debian
Posts: 347

Rep: Reputation: 34
bash Shell script to modify DNS zone file


hello everybody

i am writing a bash shell script to modify the DNS zone file. I have completed many steps, but i am stuck at the point given below.

The problem is to write a shell script/command to Convert all CNAMEs that are referencing the same domain to A records:

ie:
Code:
                               IN      A       66.223.38.187
               ftp             IN      CNAME   my-site.com.
               web             IN      CNAME   my-site.com.

     to

               ftp             IN      A       66.223.38.187
               web             IN      A       66.223.38.187
I have tried all the ways i.e. google and other Sed/awk tutorial.But could not find the solution.

Last edited by KinnowGrower; 12-10-2009 at 09:39 PM.
 
Old 12-11-2009, 02:18 AM   #2
datopdog
Member
 
Registered: Feb 2008
Location: JHB South Africa
Distribution: Centos, Kubuntu, Cross LFS, OpenSolaris
Posts: 806

Rep: Reputation: 41
Why not use perl ? http://search.cpan.org/~mschilli/DNS...S/ZoneParse.pm
 
Old 12-12-2009, 12:26 PM   #3
KinnowGrower
Member
 
Registered: May 2008
Location: Toronto
Distribution: Centos && Debian
Posts: 347

Original Poster
Rep: Reputation: 34
thanks for reply. you are right, But its requirement to do it in bash only. can any body help please?
 
Old 12-12-2009, 03:47 PM   #4
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Rep: Reputation: 16
Code:
#!/bin/bash


match=0
check=0
#put all re-occurring patterns in array "patterns"
patterns=($(awk '{print $4}' your_file_here | uniq -d))

#will read input file line by line
while read line; do
	#will check current line for every pattern in the array
	for item in ${patterns[@]}; do
		echo $line | grep -q $item
		#if there is a match, "match"=1 and replacement of CNAME will take place. "check"=1. "match" is reset to 0. line is printed to temp file
		[ $? == 0 ] && match=1
		if [ $match == 1 ]; then
			echo "$line" | sed 's/CNAME/A/' >> cname.temp
			check=1
			match=0
		fi
	done
	#if "check"=0 (no match was found), print line without modification to temp file
	[ $check = 0 ] && echo "$line" >> cname.temp
	check=0
done < your_file_here

#rename temp file to input file
mv cname.temp your_file_here

Last edited by urban.yoga.journeys; 12-12-2009 at 03:49 PM.
 
Old 12-12-2009, 08:07 PM   #5
KinnowGrower
Member
 
Registered: May 2008
Location: Toronto
Distribution: Centos && Debian
Posts: 347

Original Poster
Rep: Reputation: 34
Wao! Thanks for help

I think it is what i am in need of.

Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dns Problem Loading The Zone File Error(zone File Not Found) ramineni Linux - Newbie 1 09-14-2008 08:36 AM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 08:51 PM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 04:46 AM
[Req]: How to config WebSvr & DNS Svr from console by Bash shell script CiF Linux - Networking 2 03-22-2005 12:31 AM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration