Best method to remove from domain zone

Steini Petur

Well-Known Member
Apr 24, 2016
118
33
78
Iceland
cPanel Access Level
Root Administrator
Hey guys,

I have a question, I am aware this does not update the Serial but that is okay, because I have another way to handle that, My plan for the update of serial is the following

Code:
for i in $(cat /diskar/domainlist);
do
  echo whmapi1 addzonerecord domain=$i name=dummy123.$i. class=IN ttl=14400 type=TXT txtdata="dummy"
done
basically reading from a domain list which I have grabbed by ls through the /var/named and put into a domainlist, fair enough. I also know how I remove that, I will just do this

Code:
for x in $(cat /diskar/domainlist);
do
      for i in $(cat /var/named/$x.db | wc -l)
      do
      echo whmapi1 removezonerecord zone=$x line=$(($i - 1))
    done
done
This basically gets me where I need to go, to use the WHMAPI to add a dummy record, and remove that same added record from the zones and thus update the serial. Here is my problem

Code:
x.com.      86400   IN      NS      ns3.y.com.
x.com.      86400   IN      NS      ns4.y.com.
x.com.      14400   IN      A       127.0.0.1
I have ns3 and ns4.y.com, I have already implemented 2 new nameservers, Im doing a switch, that im deprecating these, how could SED or using any other way to find all the lines that are "IN NS ns3.y.com." with the spaces, so I can take that line and "cut/delete" it out of the file first, the whmapi does not work here becauseit depends on "line number", my method above to add and delete the dummy record hinges on adding it and deleting it right away -1 line.

I need a way to use linux terminal tool most likely to grep this line in each file not based on domain but based on that specific "IN NS ns3.y.com" find it and DELETE the whole line and its empty line space.

How can I best do this, I need to do this then add the WHMAPI and delete, to update the serial, the reason for that is because I don't want to update the serial using linux because it has to update the SERIAL but then pass it through the cluster to the main central nameservers, that is why WHMAPI comes in handy.

Any help is appreciated, a code that can help me isolate each of these lines, delete them and the leftover blank space.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
15,235
2,423
363
cPanel Access Level
Root Administrator
Hey there! While this is definitely doable, I'm extremely hesitant to post any one-liners on the Forum for all to see. One typo and then we get the blame for destroying someone's DNS :D

A combination of "find" along with a "$ sed -i '/string/d' myfile.txt" would delete the entire line containing the matching output.
 
  • Like
Reactions: Steini Petur

Steini Petur

Well-Known Member
Apr 24, 2016
118
33
78
Iceland
cPanel Access Level
Root Administrator
Hey there! While this is definitely doable, I'm extremely hesitant to post any one-liners on the Forum for all to see. One typo and then we get the blame for destroying someone's DNS :D

A combination of "find" along with a "$ sed -i '/string/d' myfile.txt" would delete the entire line containing the matching output.
Haha, I know @cPRex but any one liners you would do, I would most likely run dry-run, by doing "echo" on it or print it.. I tried the sed and I have done SED in the past using

Code:
# Replace temporarily all versions with no disable_functions
sed -i 's/disable_functions = .*/disable_functions = /' /opt/cpanel/ea-php56/root/etc/php.ini
When I needed to remove all content in the disable_functions before running pecl installer, then add again my disable_functions. However something about the large space, is giving me issues, but Ill try, Ill create a fake file to attempt to do this, see if I can somehow take everything between "IN(space)<nameserver>"


Could have been just not my day yesterday, one of those days when nothing works, maybe today is different :P
 

Steini Petur

Well-Known Member
Apr 24, 2016
118
33
78
Iceland
cPanel Access Level
Root Administrator
It was just one of those days, this was enough to get it done

Code:
sed -i '/IN.*NS.*ns[0-9].*/d' testdomain.is.db.2
Code:
cat testdomain.is.db.2 | grep SOA

testdomain.is.   86400   IN      SOA              ns3.y.com

sed -i 's/SOA.*ns[0-9].*/SOA siti.ns.y.com reports.y.com (/' testdomain.is.db.2

cat testdomain.is.db.2 | grep SOA

testdomain.is.   86400   IN      SOA     siti.ns.y.com reports.y.com (
Knowing this, I can then iterate throughit with a for loop, this gets me where I need to go, thanks for the confirmation that my SED was in the right direction, the only problem I wasn't sure was how to match all the empty space, seems ".*" does it, although looking at it it feels wrong, feels like the regex should be something else.. Im getting off easy..

This will help me get rid of lines which have "ns1 ns5 ns10" while retaining the Cloudflare type of naming "john.ns.domain.tld" :)
 
Last edited:

Steini Petur

Well-Known Member
Apr 24, 2016
118
33
78
Iceland
cPanel Access Level
Root Administrator
Note: My SED is not 100%, its reliant on the structure of your zone to be exactly SOA .... ( and a linebreak with all the retry, mname etc... below, some of our zones had all the content for odd reasons in one line off-screen behind a $ in the terminal, so I wiped out the SOA for like 90 of 2k domains, but managed to locate any entry without ";retry" with grep to make a list and fix those, here is how I did it :)

The way I fixed it is

1. Make sure you dont delete your named.bak file of course.
2. I did a "grep -L ";retry" *.db | sed s/".db"// > /broken" inside the /var/named.bak this listed all files that do not have ;retry comment in them for some reason, and separate apart from the normal design
3. now that I have the /broken file
4. I replaced them again

Code:
for i in $(cat /broken);
do
 echo yes | cp -avr /var/named.bak/$i.db /var/named/
done
5. Now I got them back in place, It's time to update the SOA of those weird files properly.

Code:
for i in $(cat /broken); 
do 
 sed -i 's/SOA.*ns[0-9].*/SOA     siti.ns.y.com.  reports.y.com. 2023021501 3600 1800 1209600 86400/' /var/named/$i.db
done
Im well aware that, it creates the same Serial on every zone, what I do now just updates those zones like before but instead of my full domainlist its just those broken, and dont mind the one liner SOA, it's recognized by WHM as a healthy record and intodns.com agrees.

Code:
for i in $(cat /broken); 
do 
 whmapi1 addzonerecord domain=$i name=dummy123.$i. class=IN ttl=14400 type=TXT txtdata="dummy"
done
followed by removing the dummy record of course

Code:
for x in $(cat /broken);
do
      for i in $(cat /var/named/$x.db | wc -l)
      do
      whmapi1 removezonerecord zone=$x line=$(($i - 1))
    done
done
And now I got em all perfect, with updated SOA serial because whmapi1 add and remove record will update it for me.
 
Last edited: