Created
September 4, 2016 07:22
-
-
Save kkamegawa/1e84ca6bcb90f6d5c9f47b16bde6a304 to your computer and use it in GitHub Desktop.
update dns record in Ubuntu 16.04 LTS (ref:http://qiita.com/nanopico-san/items/70bb58595a6c7cf77a53)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SED=/bin/sed | |
PHASE=`echo "$script" | $SED -r 's%^.*/[^/]*-(enter|exit)-.*/[^/]*$%\1%i'` | |
NSUPDATE=/usr/bin/nsupdate | |
AWK=/usr/bin/awk | |
convert_ip4_inaddr() { | |
#=- $1 ... ip4 address | |
#=- ret ... in-addr string | |
### | |
[ -n "$1" ] && echo `echo $1 | $AWK -F . '{ printf( "%d.%d.%d.%d.in-addr.arpa.", $4, $3, $2, $1 ) }'` | |
} | |
case ${reason} in | |
BOUND|RENEW|REBIND|REBOOT) | |
if [ $PHASE = "exit" ]; then | |
new_fqdn=`hostname -f` | |
new_inaddr=`convert_ip4_inaddr ${new_ip_address}` | |
if [ ! -z ${new_fqdn} ]; then | |
echo "nsupdate update add ${new_fqdn} 3600 IN A ${new_ip_address}." | |
echo "nsupdate update delete ${new_inaddr} 3600 IN PTR ${new_fqdn}." | |
$NSUPDATE <<__END__ | |
update add ${new_fqdn} 3600 IN A ${new_ip_address} | |
send | |
update add ${new_inaddr} 3600 IN PTR ${new_fqdn} | |
send | |
__END__ | |
fi | |
fi | |
;; | |
EXPIRE|FAIL|RELEASE|STOP) | |
if [ $PHASE = "enter" ]; then | |
old_fqdn=`hostname -f` | |
old_inaddr=`convert_ip4_inaddr ${old_ip_address}` | |
if [ ! -z ${old_fqdn} ]; then | |
echo "nsupdate update delete ${old_fqdn} 3600 IN A ${old_ip_address}." | |
echo "nsupdate update delete ${old_inaddr} 3600 IN PTR ${old_fqdn}." | |
$NSUPDATE <<__END__ | |
prereq yxrrset ${old_fqdn} IN A ${old_ip_address} | |
update delete ${old_fqdn} 3600 IN A ${old_ip_address} | |
send | |
prereq yxrrset ${old_inaddr} IN PTR ${old_fqdn} | |
update delete ${old_inaddr} 3600 IN PTR ${old_fqdn} | |
send | |
__END__ | |
fi | |
fi | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment