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 11-27-2021, 10:01 PM   #1
dupa
LQ Newbie
 
Registered: Nov 2021
Posts: 5

Rep: Reputation: Disabled
Question Script question for BIND


I need to or want to delete BIND DNS entries that are in File1 that match A records in File2. If the DNS entry has multiple records associated to it in File2, those need to remove too.

File1
Code:
car.example.com
truck.example.com
bike.example.com

File2
Code:
car    A    192.168.10.1
       A    192.168.10.10
       A    192.168.10.20
truck  NS   gtm1
       NS   gtm2
bike   A    192.168.10.5
       CNAME    trek.example.com

After the removal, I need or would like to add back new DNS entries into File2.
Code:
car.example.com      A    172.10.1.12
truck.example.com    A    172.10.1.15
bike.example.com     A    172.10.2.20
I tried a few grep. awk commands etc. I was able to remove the entry but not if the entry had multiple entries associated to it. However when I tried to start named.service it would fail.

Code:
grep -vwf file1 file2
Code:
grep -f <(cat file1 | sed 's/^/^/' ) file2

Last edited by dupa; 11-28-2021 at 09:07 AM. Reason: Fixed code layout
 
Old 11-28-2021, 05:13 AM   #2
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
Try this:

To get the repeated domains list:
Code:
cut -d ' ' -f 1 file1 file2 |sort |uniq -d
To get the list of domains with more than one ip:

Code:
grep "[0-9\.][0-9\.]*, [0-9\.][0-9\.]*" file2
Before executing any command read it's man page to test that it is harmless and to know what it will do

Last edited by Racho; 11-28-2021 at 05:14 AM.
 
Old 11-28-2021, 05:19 AM   #3
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
Instead of the grep I suggested above, better just:
Code:
grep "," file2
because [0-9] is looking for numbers and wouldn't work with the line
bike.example.com 192.168.10.5, trek.example.com
 
Old 11-28-2021, 07:57 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP as a start, please read this topic: https://www.linuxquestions.org/quest...gs-4175464257/
 
1 members found this post helpful.
Old 11-28-2021, 08:00 AM   #5
dupa
LQ Newbie
 
Registered: Nov 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Racho View Post
Try this:

To get the repeated domains list:
Code:
cut -d ' ' -f 1 file1 file2 |sort |uniq -d
To get the list of domains with more than one ip:

Code:
grep "[0-9\.][0-9\.]*, [0-9\.][0-9\.]*" file2
Before executing any command read it's man page to test that it is harmless and to know what it will do

Thanks for your reply. I realized that my file2 example wasn’t properly laid out as for what I’m dealing with. I updated it just now.
 
Old 11-28-2021, 09:08 AM   #6
dupa
LQ Newbie
 
Registered: Nov 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
@OP as a start, please read this topic: https://www.linuxquestions.org/quest...gs-4175464257/
Thank you. I believe I properly formatted to the guidelines now.
 
1 members found this post helpful.
Old 11-28-2021, 09:50 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Code:
grep -Fvwf <(cut -d. -f1 file1) file2

Last edited by shruggy; 11-28-2021 at 10:46 AM.
 
Old 11-28-2021, 10:30 AM   #8
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
What about this?

Code:
awk  '{
if ($0!~"^[ \t].*" ) printf("\n")
else printf("---newlinemark---")
printf("%s", $0)
} END {printf("\n")}' file2 >file_tmp

ee=($(cut -d '.' -f 1 file1))
echo ${ee[@]}
for i in ${ee[@]} ; do aa="$aa -e \"^$i\""; done
echo "$aa" |(xargs grep -v $aa file_tmp )| sed 's/---newlinemark---/\n/g' >out
echo "$aa" |(xargs grep  $aa file_tmp )| sed 's/---newlinemark---/\n/g' >deleted
rm file_tmp
What it does is:
1) joins all lines of the same record in a temporary file (awk line)
2) reads file1 into a string and prepares the grep arguments: -e "^car" -e "^truck" -e "^bike"
3) writes all records without car truck bike recovering the newlines into the file out
4) writes the removed records into the file deleted
5) removes temporary file

replace ---newlinemark--- with something you will not find in any record

Last edited by Racho; 11-28-2021 at 10:32 AM. Reason: improving code explanation
 
Old 11-29-2021, 10:17 AM   #9
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Rep: Reputation: Disabled
Quote:
Originally Posted by Racho View Post
What about this?

Code:
awk  '{
if ($0!~"^[ \t].*" ) printf("\n")
else printf("---newlinemark---")
printf("%s", $0)
} END {printf("\n")}' file2 >file_tmp

ee=($(cut -d '.' -f 1 file1))
echo ${ee[@]}
for i in ${ee[@]} ; do aa="$aa -e \"^$i\""; done
echo "$aa" |(xargs grep -v $aa file_tmp )| sed 's/---newlinemark---/\n/g' >out
echo "$aa" |(xargs grep  $aa file_tmp )| sed 's/---newlinemark---/\n/g' >deleted
rm file_tmp
What it does is:
1) joins all lines of the same record in a temporary file (awk line)
2) reads file1 into a string and prepares the grep arguments: -e "^car" -e "^truck" -e "^bike"
3) writes all records without car truck bike recovering the newlines into the file out
4) writes the removed records into the file deleted
5) removes temporary file

replace ---newlinemark--- with something you will not find in any record


interesting approach. Thanks! I did notice that it did remove more than I wanted though. anything that started with car or truck or bike removed.

example: car1, car300, bikes, etc.

Im trying to just remove the ones that are named exactly the same, nothing more.
 
Old 11-29-2021, 02:39 PM   #10
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
You are wellcome

Quote:
Im trying to just remove the ones that are named exactly the same, nothing more.
I see...

try this code
Code:
awk  '{
if ($0!~"^[ \t].*" ) printf("\n")
else printf("---newlinemark---")
printf("%s", $0)
} END {printf("\n")}' file2 >file_tmp

ee=($(cut -d '.' -f 1 file1))
echo ${ee[@]}
for i in ${ee[@]} ; do aa="$aa -e \"^$i \""; done

echo "$aa" |(xargs grep -v file_tmp )| sed 's/---newlinemark---/\n/g' >out
echo "$aa" |(xargs grep    file_tmp )| sed 's/---newlinemark---/\n/g' >deleted
rm file_tmp
I added a space in line 9 and removed $aa in grep of lines 11 and 12

Last edited by Racho; 11-29-2021 at 03:11 PM. Reason: The changes i suggested didn't work
 
Old 12-01-2021, 09:56 AM   #11
dupa
LQ Newbie
 
Registered: Nov 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Im kinda of newbie when coming to scripting ... I know some but no way an expert at it.

I could use some help on how to remove a host entry that could have (3) IP addresses then replace it with the proper (1) IP address.

I can remove 1 IP address but not if it has multiple.

I tried multiple ways and confused myself and not even sure which way was proper to remove one.

A little guidance would be much appreciated. Thank you!

Last edited by dupa; 12-01-2021 at 09:58 AM.
 
Old 12-01-2021, 10:33 AM   #12
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Sorry, I don't get what your problem is. You're matching the records not against IP addresses, but against host names, right? So what if a host has multiple IP addresses? It will be matched anyway.

E.g.
Code:
$ dig de.archive.ubuntu.com|grep ^\\w
de.archive.ubuntu.com.	597	IN	CNAME	ubuntu.mirror.tudos.de.
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.23
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.24
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.22
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.26
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.25
If you're matching against ubuntu.mirror.tudos.de, it will match all records.

It would be nice if you provided an example of what you tried to do, and what didn't work.

Last edited by shruggy; 12-01-2021 at 10:40 AM.
 
Old 12-01-2021, 10:43 AM   #13
dupa
LQ Newbie
 
Registered: Nov 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Sorry, I don't get what your problem is. You're matching the records not against IP addresses, but against host names, right? So what if a host has multiple IP addresses? It will be matched anyway.

E.g.
Code:
$ dig de.archive.ubuntu.com|grep ^\\w
de.archive.ubuntu.com.	597	IN	CNAME	ubuntu.mirror.tudos.de.
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.23
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.24
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.22
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.26
ubuntu.mirror.tudos.de.	92	IN	A	141.30.62.25
If you're matching against ubuntu.mirror.tudos.de, it will match all records.

It would be nice if you provided an example of what you tried to do, and what didn't work.

good example. how would you remove all those entries at once?

I think if I dig for it first then remove it, that may do the trick.
 
Old 12-01-2021, 11:16 AM   #14
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dupa View Post
I think if I dig for it first then remove it, that may do the trick.
I guess the output of host could be easier to parse than dig's.

Well, I was of impression that your zone file is like in the Dyn example where the host name is repeated for each IP address.

However, if it's more similar to the example in RFC 1035 then I'd use range patterns:
Code:
sed '/^VENERA\>/,/^$/d'
or
Code:
awk '/^VENERA[ \t]/,/^$/{next}1'

Last edited by shruggy; 12-01-2021 at 11:19 AM.
 
Old 12-05-2021, 02:19 PM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
\t in a character set is not standard.
I would go for
Code:
awk '/^VENERA[[:blank:]]/,/^$/{next}1'
Or
Code:
awk '$1=="VENERA"{d=1} !d; !NF{d=0}'
 
  


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
LXer: DNS: The Bind Leading the Bind LXer Syndicated Linux News 0 06-15-2006 10:33 PM
How to update BIND ./etc/bind/db.coaxcables AndeAnderson Debian 0 03-03-2006 09:35 AM
Bind 8 and Bind 9 sierra Linux - Networking 2 03-16-2005 11:15 AM
How do I revert to Bind 8 from Bind 9? digging4roots Linux - Newbie 3 06-08-2002 04:40 PM
To 'Bind' or not to 'Bind' that is the question jsurgeson Linux - Newbie 2 03-24-2002 03:10 AM

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

All times are GMT -5. The time now is 03:55 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