Skip to content

Instantly share code, notes, and snippets.

@knightshrub
Created December 29, 2017 09:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knightshrub/073472a0c5de9e19f60089c2b9cfd6e6 to your computer and use it in GitHub Desktop.
Save knightshrub/073472a0c5de9e19f60089c2b9cfd6e6 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