#!/bin/bash # This script is written by Peter Poeml and distributable under # the terms of the GPL. me=$(basename $0) conf=/etc/dnsupdate.conf usage() { cat >&2 <<-EOF Usage examples: $me add host.domain. 86400 A 10.10.100.230 $me add 230.100.10.10.in-addr.arpa. 86400 IN PTR host.domain. $me delete host.domain. $me delete 230.100.10.10.in-addr.arpa PTR $me add nickname.domain. 86400 CNAME host.domain. $me delete nickname.domain. add -b as first argument to add/delete both forward/reverse mapping. EOF } if [ $# -lt 1 ]; then usage; exit 1; fi if [ $1 = "-b" ]; then do_both=true shift else do_both=false fi args=$@ if [ -s $conf ]; then key=$(<$conf) else cat >&2 <<-EOF Error: The configuration file $conf does not exist. It needs to contain the name of the key file to use, e.g.: --# cat /etc/dnsupdate.conf /etc/Kdhcp_updater.+157+14275.private EOF exit 1 fi case $args in add*) action=add;; del*) action=del;; *) echo >&2 invalid action.; echo >&2; usage; exit 1;; esac if $do_both; then # need to transform: host.domain 86400 A 10.10.100.230 # into: 230.100.10.10.in-addr.arpa. 86400 IN PTR host.domain. # convert into array t=($args) # sanity check if [ $action = "add" ]; then case ${t[3]} in A) ;; *) echo >&2 something is wrong -- dns_class is not 'A'... args were:; echo $args; exit 1;; esac fi dns_name=${t[1]} dns_ttl=${t[2]} # if action is "delete", we need to look up the IP address to be removed if [ $action = "add" ]; then dns_data=${t[4]} else addr=$(host $dns_name) addr=($addr) case ${addr[3]} in found*) echo >&2 reverse mapping not found, not deleting.; do_both=false;; *) dns_data=${addr[3]} esac fi # split address in octets and reverse it IFS_SAVE=$IFS; IFS="."; tmp1=($dns_data); IFS=$IFS_SAVE for i in 0 1 2 3; do octet[$i]=$[ ${tmp1[$i]} ] r_dns_data=.${octet[$i]}$r_dns_data done r_dns_data=${r_dns_data#*.} r_dns_data=${r_dns_data}.in-addr.arpa # now set up the actual command line case $action in add) r_args="add $r_dns_data $dns_ttl IN PTR $dns_name";; del) r_args="delete $r_dns_data PTR";; esac fi echo command line: echo update $args $do_both && echo update $r_args echo --- echo -e "update $args \n" | nsupdate -k $key if $do_both; then echo -e "update $r_args \n" | nsupdate -k $key fi