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 ip query method #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Update(2021-7-2)

Use `curl ipify.org` to query ip, in the beginning, create two empty tmp file to avoid occurs error。

# Announcement (2017-12-21)

I no longer use Namesilo's DNS service so I cannot help any issue for this script.

-----------------------------------------------------------------
Expand Down
42 changes: 13 additions & 29 deletions namesilo_ddns.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

##Domain name:
DOMAIN="mydomain.tld"
DOMAIN="mydomain.com"

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

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

## Do not edit lines below ##

Expand All @@ -23,28 +23,12 @@ NO_IP_CHANGE_TIME=86400
##Response from Namesilo
RESPONSE="/tmp/namesilo_response.xml"

##Choose randomly which OpenDNS resolver to use
RESOLVER=resolver$(echo $((($RANDOM%4)+1))).opendns.com
##Get the current public IP using DNS
CUR_IP="$(dig +short myip.opendns.com @$RESOLVER)"
ODRC=$?
##Get the current public IP using ipify
CUR_IP=`curl -s https://api.ipify.org`

## Try google dns if opendns failed
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
##Get the current public IP
IPQUOTED=$(dig TXT +short o-o.myaddr.l.google.com @$RESOLVER)
GORC=$?
## Exit if google failed
if [ $GORC -ne 0 ]; then
logger -t IP.Check -- IP Lookup at $RESOLVER failed!
exit 1
fi
CUR_IP=$(echo $IPQUOTED | awk -F'"' '{ print $2}')
fi
# Init
touch /var/tmp/MyIPTime
touch /var/tmp/MyPubIP

##Check file for previous IP address
if [ -f $IP_FILE ]; then
Expand All @@ -56,17 +40,17 @@ fi
##See if the IP has changed
if [ "$CUR_IP" != "$KNOWN_IP" ]; then
echo $CUR_IP > $IP_FILE
logger -t IP.Check -- Public IP changed to $CUR_IP from $RESOLVER
logger -t IP.Check -- Public IP changed to $CUR_IP

##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)
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 All @@ -78,7 +62,7 @@ if [ "$CUR_IP" != "$KNOWN_IP" ]; then
else
## Only log all these events NO_IP_CHANGE_TIME after last update
[ $(date "+%s") -gt $((($(cat $IP_TIME)+$NO_IP_CHANGE_TIME))) ] &&
logger -t IP.Check -- NO IP change from $RESOLVER &&
logger -t IP.Check -- NO IP change &&
date "+%s" > $IP_TIME
fi

Expand Down