Skip to content

Instantly share code, notes, and snippets.

@patrickng
Forked from knightshrub/ddns.sh
Created December 27, 2018 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickng/4c7560068ec4a7aa88698e13b7cf3979 to your computer and use it in GitHub Desktop.
Save patrickng/4c7560068ec4a7aa88698e13b7cf3979 to your computer and use it in GitHub Desktop.
This script can be used to update a Namecheap dynamic DNS A record from a cron job. This is useful when the host does not have a static IP e.g. when sitting behind a DSL modem and being assigned an IP by the ISP via DHCP.
#!/bin/bash
# This script makes it possible to update a Namecheap dynamic DNS
# A record automatically using a cron job
# configure these
dnshost="host"
dnsdomain="example.com"
dnspw="longasspasswd"
# DNS A record is updated by issuing a GET request to this URL
CURLARGS="https://dynamicdns.park-your-domain.com/update?host=$dnshost&domain=$dnsdomain&password=$dnspw"
# if this script was run before there should be a log file
# containing last run's IP
if [ -f $(pwd)/ip.log ]; then
oldip=$(cat $(pwd)/ip.log)
else
oldip="0.0.0.0"
fi
# get the current IP
newip=$(curl -4 https://icanhazip.com/s)
# if the IP has changed since the last run then update the Namecheap A record
if [ $oldip != $newip ]; then
oldip=$newip
echo $oldip > $(pwd)/ip.log
curl $CURLARGS > $(pwd)/curl.log
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment