Help Writing Bash Script to Update Dynamic DNS


Recommended Posts

So I'm trying to write a script that I can run and check the external IP Address and if it has changed to send the new IP Address to my DNS provider, but I can't seem to get this to work:

 

NOWIPADDR="wget -qO- http://ipecho.net/plain ; echo"
GETIPADDR="/var/log/externalip.log"

    if [ -f $NOWIPADDR ]
    then
       if [ `cat $NOWIPADDR` = `curl $GETIPADDR` ]
       then
           echo "no change in IP."
       else
           $GETIPADDR > $NOWIPADDR
           curl -u "[USERNAME]:[PASSWORD]" "https://iwantmyname.com/basicauth/ddns?hostname=[HOSTNAME]&myip="
        fi
    else
        curl $GETIPADDR >> $NOWIPADDR
    fi

When I run bash -x ddns, I get the following errors:

+ NOWIPADDR='wget -qO- http://ipecho.net/plain ; echo'

+ GETIPADDR=/var/log/externalip.log

+ '[' -f wget -qO- http://ipecho.net/plain ';' echo ]'

ddns: line 7: [: too many arguments

+ curl /var/log/externalip.log

ddns: line 17: $NOWIPADDR: ambiguous redirect

 

I'm still learning to write this stuff, so be gentle  :p

Link to comment
Share on other sites

Never mind. I found another script and adapted it to my needs and it works :) 

 

For other's reference:

#!/bin/bash
#
# DynDNS for iwantmyname
# including check between external IP and existing DNS entries in the internet
#

# Domain parameters
DOMAIN=""
SUBDOMAIN=""
USERNAME=""
PASSWORD=""

# get external IP
GETEXTIP=$(curl ipecho.net/plain ; echo | awk '{print $6}' | cut -f 1 -d "<")

# get external IP from DNS
GETDNSIP=$(dig +short $SUBDOMAIN.$DOMAIN @8.8.8.8)

# DEBUG COMMANDS
#echo "EXT: "$GETEXTIP
#echo "DNS: "$GETDNSIP

# Update DNS entry

if [ "$GETDNSIP" != "$GETEXTIP" ]; then
 curl -0 --silent -u "$USERNAME:$PASSWORD" "https://iwantmyname.com/basicauth/ddns?hostname=$SUBDOMAIN.$DOMAIN&myip=$GETEXTIP" > /dev/null
fi
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.