-2

I have a duplicate in DNS record when adding new instance. My goal is to use "nsupdate" to be able to delete A record by IP if new instance IP is found. This is what I did. However, it didn't delete any duplicate when ran this script. what will be the best walk around?

domain=${domainname}
nodename=(hostname | awk -F. '{print $1}')
local_ip=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')     
nslookup ${local_ip} | grep SERVFAIL
if [[ $? < 0 ]] ; then
echo "Record already exists"
echo -e "update  ${local_ip} IN A"
else
echo "creating forward Record: ${codename}.${domain}"
echo -e "update add ${nodename}.${domain} 3600 A ${local_ip}\nsend" | nsupdate -g
fi 
Ade
  • 23
  • 2
  • 8
  • There seem to be several typos in your code as well as what appear to be logical inconsistencies. For instance, when updating you don't pipe your command to ```nsupdate``` on line 7 – smokes2345 Mar 12 '18 at 14:53
  • FWIW, a lot of your problems could be fixed with a simple proofreading. – smokes2345 Mar 12 '18 at 15:23
  • *"This is what I did. Please help"* - Please help with what? You did not state a problem or error. (And *+1* for asking a Bash question that has to do with programming and development). – jww Mar 12 '18 at 17:55
  • I stated the problem . can you please read what intend to achieve with my script . – Ade Mar 12 '18 at 19:47

1 Answers1

0

It looks like you have several problems

Grep will not return a value less than 0, so your code to do your update will never run. Even if it did run, you're not echoing that command to nsupdate, so your record still wouldn't be changed. Also, there are several typos in your code

domain=${domainname}
nodename=(hostname | awk -F. '{print $1}')
local_ip=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')     
if nslookup ${local_ip} | grep SERVFAIL; then
    echo "creating forward Record: ${nodename}.${domain}"
    echo -e "update add ${nodename}.${domain} 3600 A ${local_ip}\nsend" | nsupdate -g
else
    echo "Record already exists"
    echo -e "update  ${local_ip} IN A" | nsupdate -g
fi 

I've never used nsupdate personally, but i'm assuming that you meant to send your update command to it in the same fashion as your 'add'.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
smokes2345
  • 190
  • 1
  • 12
  • If the new instance IP already exist in A record(DNS) , the script should delete it before adding the new instance IP . If I don't do that , the two instance will have same IP address – Ade Mar 12 '18 at 15:51
  • If you have two instances using the same IP on the same subnet then DNS is the least of your worries. Also, your script, as far as i can tell, makes no *attempt* to delete anything. If you have a problem i don't mind helping, but this isn't a development service. Your 'question' is poorly framed in all sorts of ways, the biggest (though not the solitary) being the poorly written script wrought with simple typographical errors. Instead of posting here you should have started with ```man nsupdate``` because everything you need to know, combined with the code i wrote, is in there. – smokes2345 Mar 13 '18 at 18:25