Created
October 8, 2017 03:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :) )