1 #!/bin/bash
 2 
 3 ################### Dynamic DNS update script ####################
 4 # Update the IP for a domain registered at namecheap.com
 5 # Uses the following url per the namecheap documentation:
 6 # https://dynamicdns.park-your-domain.com/update?host=[host]&domain=[domain_name]&password=[ddns_password]&ip=[your_ip]
 7 ##################################################################
 8 # made with <3 by ~inthreedee ( https://tilde.town/~inthreedee/ )
 9 ##################################################################
10 
11 domain="example.com"
12 password="password"
13 # place either a subdomain in host, www, or use @ to update the bare domain
14 # use * to update the wildcard subdomain
15 # run multiple times to update, for example, both the bare domain and www
16 host="subdomain"
17 # use a temporary file to store the last known IP
18 # prevents unnecessary load on the update server
19 ipfile="/tmp/dns_last_ip"
20 # create the file if it doesn't exist to avoid file not found errors
21 touch "$ipfile"
22 lastip="$(cat "$ipfile")"
23 responsefile="/tmp/dns_response"
24 greperror="/tmp/dns_greperror"
25 
26 echo "$(date +"%Y-%m-%d %H:%M:%S")"
27 echo " Beginning DNS update..."
28 
29 # Perform a single udp packet IP lookup. Fastest method to do this
30 ip="$(dig -4 +short myip.opendns.com @resolver1.opendns.com 2> "$responsefile")"
31 # If OpenDNS fails, try akamai
32 if [ "$?" -ne 0  ] || [ -z "$ip"  ]; then
33   ip="$(dig -4 +short @ns1-1.akamaitech.net ANY whoami.akamai.net 2>> "$responsefile")"
34 fi
35 # If akamai fails, try google as a last resort
36 if [ "$?" -ne 0  ] || [ -z "$ip"  ]; then
37   ip="$(dig -4 +short @ns1.google.com TXT o-o.myaddr.l.google.com | tr -d \" 2>> "$responsefile")"
38 fi
39 
40 # If all the lookups failed, abort
41 if [ "$?" -ne 0  ] || [ -z "$ip"  ]; then
42         # Lookup failed. Abort and report the problem
43         echo " **IP lookup failed** OpenDNS resolver might not be responding?"
44         echo " Error recieved: '"$(cat "$responsefile" )"'"
45         echo "DNS update aborted!"
46 
47         # If postfix sendmail is available, send a message to the root mailbox
48         if [ "$(command -v sendmail)" ]; then
49                 echo -e "Subject: DNS Update Script Error\nIP lookup failed. OpenDNS resolver might not be responding.\n\nIP received: '$ip'\nError received: '"$(cat "$responsefile")"'" | /usr/sbin/sendmail root
50         fi
51 elif [ "$ip" == "$lastip" ]; then
52         echo " Public IP is still $ip, no changes detected."
53         echo "DNS check complete!"
54 else
55         echo " Last IP was $lastip. Current IP is $ip. Updating DNS..."
56 
57         echo " - Updating DNS for $host.$domain..."
58         echo "$(curl -s "https://dynamicdns.park-your-domain.com/update?host="$host"&domain="$domain"&password="$password"")" > "$responsefile"
59 
60         # Search for errors in the response we received
61         # grep returns 0 if matches were found and 1 if no matches were found
62         grep -i "errors" "$responsefile" > /dev/null 2> "$greperror"
63         check="$?"
64         if [ "$check" -eq 0 ]; then
65                 # The update failed. Report the problem
66                 # Don't update the IP file so we try again next time
67                 echo " **Error during IP update**"
68                 echo " $(cat "$responsefile")"
69 
70                 # If postfix sendmail is available, send a message to the root mailbox
71                 if [ "$(command -v sendmail)" ]; then
72                         echo -e "Subject: DNS Update Script Error\nError during IP update. Response received:\n\n"$(cat "$responsefile")"" | /usr/sbin/sendmail root
73                 fi
74         elif [ "$check" -eq 1 ]; then
75                 # The update succeeded, so update the IP file
76                 echo "$ip" > "$ipfile"
77         else
78                 # An error occured with grep. Report the problem
79                 echo " **grep error during update. This should not happen!**"
80                 echo " $(cat "$greperror")"
81 
82                 # If postfix sendmail is available, send a message to the root mailbox
83                 if [ "$(command -v sendmail)" ]; then
84                         echo -e "Subject: DNS Update Script Error\nError while grepping. This should not happen! Error received:\n\n"$(cat "$greperror")"" | /usr/sbin/sendmail root
85                 fi
86         fi
87         echo "DNS update complete!"
88 fi