Jump to content



Photo

Help Writing Bash Script to Update Dynamic DNS

Answered Go to the full post

  • Please log in to reply
1 reply to this topic

#1 Hurmoth

Hurmoth

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 09-March 03
  • Location: Virginia

Posted 21 February 2014 - 16:19

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



Best Answer Hurmoth , 21 February 2014 - 17:46

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
Go to the full post



#2 OP Hurmoth

Hurmoth

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 09-March 03
  • Location: Virginia

Posted 21 February 2014 - 17:46   Best Answer

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