Last active
January 15, 2024 04:06
-
-
Save cokert/bf102b5997e1c89861449074ea447951 to your computer and use it in GitHub Desktop.
Update DNSimple subdomain with current public facing IP as returned by ifconfig.me along with a systemd service and timer file
This file contains hidden or 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
TOKEN=<DNSIMPLE-TOKEN> | |
ACCOUNT=<ACCOUNTID> | |
ZONE=<ZONEID-OR-DOMAINNAME> | |
RECORD=<RECORDID> |
This file contains hidden or 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
[Unit] | |
Description=Update IP Address with DNSimple | |
Wants=update-ip.timer | |
[Service] | |
Type=oneshot | |
ExecStart=<PATH-TO-SCRIPT>/update-ip.sh -t $TOKEN -a $ACCOUNT -z $ZONE -r $RECORD | |
EnvironmentFile=<PATH-TO-PARAMETERS-FILE>/.update-ip-parameters | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or 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/zsh | |
# makes a DNSimple API request to the following URL to update a particular subdomain with the IP returned | |
# by making a request to ifconfig.me | |
# https://api.dnsimple.com/v2/{{Account ID}}/zones/{{Zone ID}}/records/{{Zone Record ID}} | |
# see also https://www.postman.com/cokert/workspace/dnsimple/collection/7188053-d529d5c8-1d38-4e70-bc58-cd41db702711 | |
# for example requests to find the zone and record ids | |
while getopts a:z:r:t: flag | |
do | |
case "${flag}" in | |
a) account_id=${OPTARG};; | |
z) zone_id=${OPTARG};; | |
r) record_id=${OPTARG};; | |
t) token=${OPTARG};; | |
esac | |
done | |
if [[ -z "$account_id" || -z "$zone_id" || -z "$record_id" || -z "$token" ]] | |
then | |
echo "This script updates a dnsimple hosted domain\'s subdomain with the current public IP as" | |
echo "returned by a request to \`ifconfig.me\`" | |
echo " -a: Your account ID (listed in the address bar of dnsimple.com)" | |
echo " -z: The zone id of the domain to update (can be the domain name)" | |
echo " -r: The record ID of the subdomain to update" | |
echo " -t: Your dnsimple user token" | |
echo "See https://developer.dnsimple.com/v2/zones/records/ for more info" | |
exit 1 | |
fi | |
public_ip=$(curl ifconfig.me) | |
# public_ip=31.31.31.34 | |
set -x | |
curl -H 'Authorization: Bearer '$token'' \ | |
-H "Content-Type: application/json" \ | |
-X PATCH \ | |
-d '{"content": "'"$public_ip"'"}' \ | |
https://api.dnsimple.com/v2/$account_id/zones/$zone_id/records/$record_id | |
set +x |
This file contains hidden or 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
[Unit] | |
Description=Update IP Address with DNSimple every 30 minutes | |
Requires=update-ip.service | |
[Timer] | |
Unit=update-ip.service | |
OnCalendar=*-*-* *:00/30:00 | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that running the systemd timer like will result in your DNSimple token being logged/visible with
journalctl
... ¯_(ツ)_/¯