Skip to content

Instantly share code, notes, and snippets.

@mstancombe
Created October 8, 2017 03:07
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 mstancombe/3ec941e9c055b76a3e3c3e8016fb363b to your computer and use it in GitHub Desktop.
Save mstancombe/3ec941e9c055b76a3e3c3e8016fb363b to your computer and use it in GitHub Desktop.
Simple script to refresh a DNSimple zone record from an LEDE wan IP, for dynsns behaviour with DNSimple
#!/bin/sh
###############
# User Variables, These MUST be set
###############
# Credentials
# Be sure that if there are restricted characters like " in the password that they are escaped, eg, ="A\"B"
# TODO: storing in a built in encrypted location, to prevent accidental copy including credentials.
DNSimpleUser="yourusername@yourdomain.com"
DNSimplePassword="yourpassword"
# The zone record path. This should be the form of {accountid}/zones/{domain name}/records/{record id}
# and can be easily retrieved by logging into dnsimple web interface and editing the record, the url
# will contain all this information in this same format.
DNSimpleZoneApiPath="12345/zones/mysuperdomain.com/records/12345678"
##########
# Constants for the script
##########
DNSimpleApiBase="https://api.dnsimple.com/v2"
########
# Get the lede wan ip address.
########
wan_ip=`ubus call network.interface.wan status |
grep \"address\" |
grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}';`
echo Retrieved Wan Ip: $wan_ip
########
# Check the dnsimple host entry.
########
dns_ip=`curl \
-silent \
-u $DNSimpleUser:$DNSimplePassword \
-H 'Content-Type: application/json' \
$DNSimpleApiBase/$DNSimpleZoneApiPath 2>&1 |
grep -oE \"content\":\"[0-9.]*\" |
grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}';`
echo Retrieved DNS Ip: $dns_ip
########
# If it is different, update it to wan ip
########
if [ $dns_ip != $wan_ip ]; then
echo DNS entry differs, updating dns to wan ip..
updated_ip=`curl \
-silent \
-u $DNSimpleUser:$DNSimplePassword \
-X PATCH \
-H 'Content-Type: application/json' \
--data '{"content": "'$wan_ip'"}' \
$DNSimpleApiBase/$DNSimpleZoneApiPath |
grep -oE \"content\":\"[0-9.]*\" |
grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}';`
echo $updated_ip
if [ $updated_ip = $wan_ip ]; then
echo DNS entry updated. Everything is Awesome!
else
echo Couldn\'t update DNS Zone.
# TODO: fail like a shell script should. log? email?
fi
else
echo DNS already up to date, nothing to do.
fi
@mstancombe
Copy link
Author

Simple script to update DNSimple host record file. To use it, modify the parameters at the top, copy it into a file (I put it in /usr/bin/update_dnsimple_host.sh) then chmod u+x and run it. Feedback from experienced script authors welcome.. I'm not a linux guy (yet :) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment