3

We know that we can update a record (its IP) by doing these steps:

nsupdate
server ns.bar44.com
zone bar44.com
update delete somehost.bar44.com. A
update add somehost.bar44.com. 86400 A 10.10.10.1
show
send
  1. As we can see we know that the somehost.bar44.com. is exist in the DB, this will work if I want to update the IP of an existing Record, but what if I want to change the hostname not the IP. E.g., I want to make 10.10.10.1 the IP of somehost22.bar44.com. what will let me know that IP is already taken by somehost.bar44.com.?

  2. Is this a way to delete the whole DB of a certain zone using nsupdate?

  • What's the issue you are facing ??, what's the issue if two hosts has same ip ? – Rahul Patil May 25 '14 at 10:11
  • I want to give a machine with a specific IP different names which I extract from a dynamic DB – Networker May 25 '14 at 10:15
  • so you want to find let say somehost22.bar44.com and delete the same and update somehost.bar44.com ? is it ? – Rahul Patil May 25 '14 at 10:25
  • 1
    we can make shell script, which will execute AXFR query to your dns server for checking that IP and domain exists or not and performed accordingly – Rahul Patil May 25 '14 at 10:40
  • 1
    dear I'll be back after half an hour.. and will try to create that script in my side and update you soon.. – Rahul Patil May 25 '14 at 10:49
3

DISCLAIMER: use this script on your own risk


What it does ?

As O/P wants, lat say he want to add somedomain.bar44.com and the somedomain44.bar44.com exists in zone then it should remove somedomain44.bar44.com and should be add somedomain.bar44.com into zone. this process will done using this script. Tested on Ubuntu bind9.

in short it will add xyz.bar.com if not exist and if exists then it will remove (xyz*.bar.com) re-add with new information provided by you.

Script :

#!/bin/bash
#
## Update DNS Records Interactive
## Rahul Patil <http://www.linuxian.com>

#
## Functions
#

ask() {

    while [[ $ans == "" ]]
    do
        read -p "${@}"  ans
    done

    echo $ans
}

forward_zone_update() { 
    local rr=${@}
    echo "
    server $DNS_SERVER
    zone $DNS_ZONE
    update add $rr
    show
    send" | nsupdate
}

delete_record() { 
    local rr=${@}
    echo "
    server $DNS_SERVER
    zone $DNS_ZONE
    update delete $rr
    show
    send" | nsupdate
}


#
## Global Variable
#
DNS_IP="127.0.0.1"
DNS_SERVER="ns1.rahul.local"
DNS_ZONE="rahul.local"
DIG_CMD='dig +noquestion +nocmd +nostat +nocomments'

update_rr_a=$( ask "Enter FQDN of Record (Ex. xyz.${DNS_ZONE}) :-")
update_rr=$( ask "Enter IP of Record :-")
found_rr=$($DIG_CMD @${DNS_IP} AXFR ${DNS_ZONE} | grep  ^"${update_rr_a%.$DNS_ZONE}" | tee /tmp/rr.tmp )

echo "Checking ${update_rr_a}..."

if [[ -z "${found_rr}" ]] 
then
    echo "${update_rr_a} does exists"
    echo "${update_rr_a} adding to ${DNS_ZONE}"
    forward_zone_update "${update_rr_a} 86400 IN A ${update_rr}"
    echo "Done!!"
else
    echo "${update_rr_a} already exists"
    ans=$(ask "Do you want to Delete RR and want to re-add(y/n?)")
    case $ans in
        [yY]|[yY][eE][sS]) while read r; 
                   do delete_record $r ; 
                               done < /tmp/rr.tmp ;;

        [nN]|[nN][oO])     exit 1 ;;
    esac
    forward_zone_update "${update_rr_a} 86400 IN A ${update_rr}"
    echo "Done!!"
fi
  • thanks but why the script will be risky? – Networker May 25 '14 at 13:21
  • As per your requirement it's Okay, but suppose somebody want to use and requirement bit different, then it will be risky, suppose he want to remove only xyz11.bar.com not xyz.bar.com, then he will lost xyz.bar.com info only have last updated xyz11.bar.com, but after testing on staging anyone can easily modify and use the same.. I hope you understood – Rahul Patil May 25 '14 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.