Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update namesilo_ddns.sh #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions namesilo_ddns.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
#!/bin/bash


#note
#apt-get install libxml2-utils
#apt-get install dnsutils

#note: dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com
#note: dig +short myip.opendns.com @resolver1.opendns.com
# two ways to get WAN ip
##Domain name:
DOMAIN="mydomain.tld"
DOMAIN="domain.com"

##Host name (subdomain). Optional. If present, must end with a dot (.)
HOST="subdomain."
HOST="sub_domain"

##APIKEY obtained from Namesilo:
APIKEY="c40031261ee449037a4b4"
APIKEY="key"

## Do not edit lines below ##

if [ ! -d "/home/tmp" ]; then
echo "mkdir -p /home/tmp"
mkdir -p /home/tmp
fi

##Saved history pubic IP from last check
IP_FILE="/var/tmp/MyPubIP"
IP_FILE="/home/tmp/MyPubIP${HOST}"

##Time IP last updated or 'No IP change' log message output
IP_TIME="/var/tmp/MyIPTime"
IP_TIME="/home/tmp/MyIPTime${HOST}"

##How often to output 'No IP change' log messages
NO_IP_CHANGE_TIME=86400

##Response from Namesilo
RESPONSE="/tmp/namesilo_response.xml"
RESPONSE="/home/tmp/namesilo_response.xml"

##Choose randomly which OpenDNS resolver to use
RESOLVER=resolver$(echo $((($RANDOM%4)+1))).opendns.com
RESOLVER=resolver$(echo $((($RANDOM%3)+1))).opendns.com
##Get the current public IP using DNS
CUR_IP="$(dig +short myip.opendns.com @$RESOLVER)"
ODRC=$?
Expand All @@ -34,7 +47,7 @@ if [ $ODRC -ne 0 ]; then
logger -t IP.Check -- IP Lookup at $RESOLVER failed!
sleep 5
##Choose randomly which Google resolver to use
RESOLVER=ns$(echo $((($RANDOM%4)+1))).google.com
RESOLVER=ns$(echo $((($RANDOM%3)+1))).google.com
##Get the current public IP
IPQUOTED=$(dig TXT +short o-o.myaddr.l.google.com @$RESOLVER)
GORC=$?
Expand All @@ -54,19 +67,19 @@ else
fi

##See if the IP has changed
if [ "$CUR_IP" != "$KNOWN_IP" ]; then
echo $CUR_IP > $IP_FILE
if [ "$CUR_IP" != "$KNOWN_IP" ]; then
logger -t IP.Check -- Public IP changed to $CUR_IP from $RESOLVER

##Update DNS record in Namesilo:
curl -s "https://www.namesilo.com/api/dnsListRecords?version=1&type=xml&key=$APIKEY&domain=$DOMAIN" > $DOMAIN.xml
RECORD_ID=`xmllint --xpath "//namesilo/reply/resource_record/record_id[../host/text() = '$HOST$DOMAIN' ]" $DOMAIN.xml | grep -oP '(?<=<record_id>).*?(?=</record_id>)'`
RECORD_ID=`xmllint --xpath "//namesilo/reply/resource_record/record_id[../host/text() = '$HOST.$DOMAIN' ]" $DOMAIN.xml | grep -oP '(?<=<record_id>).*?(?=</record_id>)'`
curl -s "https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=xml&key=$APIKEY&domain=$DOMAIN&rrid=$RECORD_ID&rrhost=$HOST&rrvalue=$CUR_IP&rrttl=3600" > $RESPONSE
RESPONSE_CODE=`xmllint --xpath "//namesilo/reply/code/text()" $RESPONSE`
case $RESPONSE_CODE in
300)
echo $CUR_IP > $IP_FILE
date "+%s" > $IP_TIME
logger -t IP.Check -- Update success. Now $HOST$DOMAIN IP address is $CUR_IP;;
logger -t IP.Check -- Update success. Now $HOST.$DOMAIN IP address is $CUR_IP;;
280)
logger -t IP.Check -- Duplicate record exists. No update necessary;;
*)
Expand Down