Skip to content

Instantly share code, notes, and snippets.

@patrickng patrickng/ddns.sh forked from knightshrub/ddns.sh
Created Dec 27, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.