Dynamic DNS for Namecheap script


6 posts in this topic Last Reply

Recommended Posts

Hi guys,

 

I'm a fresh Unraid user, built a small server that has been up an running for almost a week now. I had a script to update DNS records for a domain hosted with Namecheap that I customized for the Unraid server I thought I'd share, maybe someone else will find some use from it. It uses the Unraid notification system to send an e-mail on updates and if an error was encountered during the update.

I use it in conjunction with the CA User Scripts plugin and have it setup to run every 15 minutes.

 

Requirements (I installed these through Nerd Pack)

  • bind
  • lmdb
  • json-c

 

How to use:

I just made a new script through the CA User Scripts interface, and pasted the code below into the script file. Then I chose to use a custom schedule and entered "*/15 * * * *" to make it run every 15 minutes.

  1. Put the hosts you want checked and updated in the hosts variable, each host encapsulated in double quotes and separated with spaces on line 2
  2. Put your domain name in the domain variable on line 3
  3. Put your DNS password that you get from the Namecheap control panel for your domain on line 4
#!/bin/bash
hosts=( "@" "host1" "host2" "host3" ) # Enter your hosts in quotes separated by a space. The first and last characters within the parenthesis need to be spaces.
domain="domain.com" # Your domain name
password="your-dns-password" # The Dynamic DNS password from Namecheap, you'll find it in the control panel for your domain

# Set up some variables
hosts_updated=""
hosts_unchanged=""

# Fetch your WAN IP address and store in a variable
new_ip=$(curl -s http://dynamicdns.park-your-domain.com/getip)

# Loop through all the hosts and check if they need updating
for host in ${hosts[*]}
do
        if [[ $host == "@" ]]; then
                fulldomain=$domain
        elif [[ $host != "@" ]]; then
                fulldomain=$host.$domain
        fi

        # Check if the A record is the same as your current WAN IP, if it is just continue with the next host
        current_ip=$(dig $fulldomain @8.8.8.8 +short)
        if [[ $new_ip == $current_ip ]]; then
                hosts_unchanged+="${fulldomain}\n"
                continue
        fi

        # Try to update the DNS record and store the response
        response=$(curl -s "https://dynamicdns.park-your-domain.com/update?host=$host&domain=$domain&password=$password&ip=$new_ip")
        
        # Check if there's errors in the response
        regex="<ErrCount>([0-1])<\/ErrCount>"
        if [[ $response =~ $regex ]]; then
                if [[ ${BASH_REMATCH[1]} == "0" ]]; then
                        hosts_updated+="${fulldomain}\n"

                elif [[ ${BASH_REMATCH[1]} == "1" ]]; then
                        # Send mail through Unraid notification system. If you want notifications in the web browser, remove the "-t" at the end.
                        /usr/local/emhttp/webGui/scripts/notify -e "DDNS Namecheap" -s "DNS Update Error ${fulldomain}" -i "warning" -m "An error was encountered when trying to update ${fulldomain}, the response was:\n${response}" -t
                        # Log to console
                        echo -e "An error was encountered when trying to update ${fulldomain}, the response was:\n${response}"
                fi
        fi
done

# If any hosts have been updated, send an e-mail through the Unraid notification system.
if [[ ! -z "$hosts_updated" ]]; then
        message="The following hosts just had their DNS updated with IP ${new_ip}:\n${hosts_updated}"
	
        # If any of the hosts were unchanged, mention them in the message.
        if [[ ! -z "$hosts_unchanged" ]]; then
                message+="\nThe following hosts were left unchanged:\n${hosts_unchanged}"
        fi

        # Send mail through Unraid notification system. If you want notifications in the web browser, remove the "-t" at the end.
        /usr/local/emhttp/webGui/scripts/notify -e "DDNS Namecheap" -s "DNS Updated: ${new_ip}" -m "${message}" -t
        # Log to console
        echo -e "${message}"
fi

# If no hosts have been updated, just echo to the console.
if [[ -z "$hosts_updated" ]]; then
        echo "No hosts have been updated"
fi

This script could quite easily be modified to work with other DNS services that offers a similar technique to Namecheap if you wish.

 

Hopefully someone else will find some use from it

 

I think the script is pretty much done, but if I decide to update it I will push changes to Github, you can find it here

 

Thanks for looking

//Mattias

Edited by maetthew
Link to post
  • 1 month later...
  • 2 months later...

Hey @maetthew Trying to get this working and I am getting the following error:

 

Script location: /tmp/user.scripts/tmpScripts/Namecheap DDNS/script
Note that closing this window will abort the execution of this script
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update www.urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update hedgedoc.urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update rutorrent.urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
No hosts have been updated

 

What am I doing wrong? I got those 3 packages installed from the NerdPack. Using the RAW script you have on your Github page.

Edited by plantsandbinary
Link to post
20 hours ago, plantsandbinary said:

Hey @maetthew Trying to get this working and I am getting the following error:

 


Script location: /tmp/user.scripts/tmpScripts/Namecheap DDNS/script
Note that closing this window will abort the execution of this script
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update www.urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update hedgedoc.urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
nslookup: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
An error was encountered when trying to update rutorrent.urdnshere.download, the response was:
SETDNSHOSTeng1No Records updated. A record not Found;1380091No updates; A record not Found;true
No hosts have been updated

 

What am I doing wrong? I got those 3 packages installed from the NerdPack. Using the RAW script you have on your Github page.

Hi,

 

Never seen such a message. Did you set up the variables on line 2-4 in the script?

 

Also have you set up your A records with Namcheap? You need to manually set up the records the first time, then the script will update the records when necessary.

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.