06. January 2012 · Comments Off on Quickly update or delete a zonefile host entry. · Categories: Linux Scripts · Tags: , ,
#!/bin/bash
## Created By: deaves
# Quickly update or delete a zonefile host entry.
#
## Requires: dnsutils, netcat, sshfp (python script)
 
DOMAIN="foo.com"
SERVER="ns.foo.com"
 
##### Begin Script #####
 
function DELETE () {
### Purge DNS entry for HOST ###
nsupdate < < _EOF_
  server ${SERVER}
  update delete ${HOST}.${DOMAIN} A
  update delete ${HOST}.${DOMAIN} SSHFP
  send
_EOF_
}
 
function UPDATE () {
### Update DNS record for HOST ###
nsupdate << _EOF_
  server $SERVER
  update add $HOST.$DOMAIN 3600 A $IPv4
  `netcat -zv $IPv4 22 2> /dev/null && sshfp -s $IPv4 2> /dev/null | awk '{print "update add '$HOST.$DOMAIN' 3600",$2,$3,$4,$5,$6}'`
  send
_EOF_
}
 
function usage () {
  ### Display the script arguments.
  printf "Usage: $0 [-du] -h <host> -i <ip>\n\n"
  printf "Requires one option!\n"
  printf "\t-d: Delete DNS <host> A and SSHFP records\n"
  printf "\t-u: Update DNS </host><host> A and SSHFP records\n\n"
}
 
while getopts "duh:i:" ARG; do
  case "${ARG}" in
    d) [ -z $ACTION ] && { ACTION="D"; };;
    u) [ -z $ACTION ] && { ACTION="U"; };;
    h) HOST="$OPTARG";;
    i) IPv4="$OPTARG";;
    ?) echo "Invalid option -$OPTARG"; exit 1;;
  esac
done 2> /dev/null
 
[ -n "$ACTION" ] && { echo "$ACTION: $HOST ($IPv4)" ; }
 
if [ "$ACTION" == "U" ]; then
  [ -z "$IPv4" ] && { echo "Error: Missing IP" && exit 1; }
  DELETE
  UPDATE
elif [ "$ACTION" == "D" ]; then
  [ -z "$HOST" ] && { echo "Error: Missing HOST" && exit 1; }
  DELETE
else
  usage && exit 1;
fi</host></ip></host>