Created
May 10, 2018 17:40
-
-
Save dentex/bba78731de9c9ab01947ef5b3950c680 to your computer and use it in GitHub Desktop.
Bash script to update a dnsdynamic.org domain with your machine's current external IP.
This file contains hidden or 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 | |
# script name | |
SC=`basename $0` | |
############### CONFIG ################ | |
user="<<DNSDYNAMIC-ACCOUNT-EMAIL>>" | |
pw="<<DNSDYNAMIC-ACCOUNT-PASSWD>>" | |
domain="<<DNSDYNAMIC-DOMAIN-TO-UPDATE>>" | |
log="/home/<<USER-NAME>>/bin/last_ext_ip" | |
####################################### | |
# external IP services | |
read -d '' urls << 'END' | |
http://myip.dnsdynamic.org/ | |
http://whatismyip.akamai.com/ | |
https://ident.me/ | |
https://ip.tyk.nu/ | |
https://tnx.nl/ip | |
https://wgetip.com/ | |
END | |
function check_network { | |
ping -q -c 1 -w 3 8.8.8.8 > /dev/null 2>&1 | |
if [ "$?" -ne 0 ]; then | |
echo "[$(date '+%x %X')] [$SC] No network connection. Exiting" | |
exit 1 | |
fi | |
} | |
function update_dnsdynamic { | |
# check URLs until first IP is found | |
for url in $urls; do | |
ext_ip=$(curl -m 5 -L -s $url) | |
if [ ! -z $ext_ip ]; then | |
echo -e "[$(date '+%x %X')] [$SC] $url -> $ext_ip" | |
break | |
fi | |
done | |
# check if the found IP has been already logged/updated | |
if [ -f $log ]; then | |
last_ext_ip=$(cat $log) | |
fi | |
# if IP has not been logged, update the dnsdynamic.org domain | |
if [ "$ext_ip" != "$last_ext_ip" ]; then | |
echo -e "[$(date '+%x %X')] [$SC] Running curl to upgrade dnsdynamic.org" | |
out=$(curl -s --user "$user:$pw" "https://www.dnsdynamic.org/api/?hostname=$domain&myip=$ext_ip") | |
response=$(echo $out | cut -d " " -f1) | |
echo -e "[$(date '+%x %X')] [$SC] -> $out" | |
if [ $? -eq 0 ]; then | |
if [ "$response" == "good" ]; then | |
echo -e "[$(date '+%x %X')] [$SC] dnsdynamic.org successfully updated" | |
elif [ "$response" == "nochg" ]; then | |
echo -e "[$(date '+%x %X')] [$SC] no change at dnsdynamic.org" | |
fi | |
# store IP for later reference | |
echo $ext_ip > $log | |
else | |
echo -e "[$(date '+%x %X')] [$SC] dnsdynamic.org IP upgrade *failed*" | |
fi | |
else | |
echo -e "[$(date '+%x %X')] [$SC] external IP unchanged" | |
fi | |
} | |
check_network | |
update_dnsdynamic |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment