Skip to content

Instantly share code, notes, and snippets.

@nna774
Last active May 1, 2019 00:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nna774/9447e6336acb8e4d411e6c0e8d82f8f8 to your computer and use it in GitHub Desktop.
Save nna774/9447e6336acb8e4d411e6c0e8d82f8f8 to your computer and use it in GitHub Desktop.
#! /bin/sh -xe
IP=`curl https://ipinfo.io/ip 2> /dev/null`
EMAIL="hie@natto"
TOKEN="huehuehuehue"
LOG=
PTRS=
changed() {
DOMAIN=$1
PTRS=`nslookup $1 | sed -ne 's/Address: //p'`
[ $IP != $PTRS ]
}
update() {
ZONE=$1
RECORD=$2
DOMAIN=$3
if `changed $3`; then
echo "Address Changed! from $PTRS to $IP" >&2
LOG=`curl -X PUT -H "Content-Type: application/json" -H "X-Auth-Email: ${EMAIL}" \
-H "X-Auth-Key: ${TOKEN}" \
https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD} \
--data "{\"zone_id\": \"${ZONE}\", \"type\": \"A\", \"name\": \"${DOMAIN}\", \"content\": \"${IP}\"}"`
else
echo "Address not Changed." >&2
fi
}
update "tatata" "sanukimiko" "mikan.nina.xxx"
if [ "x$LOG" != "x" ]; then
TEMP=`mktemp`
echo "From: Update DDNS log <update-ddns-log@ushio.nna774.net>" > "$TEMP"
echo "To: update-ddns@localhost" >> "$TEMP"
echo "Subject: Update DDNS log" >> "$TEMP"
echo >> "$TEMP"
echo "$LOG" | jq . >> "$TEMP"
sendmail "update-ddns@localhost" < "$TEMP"
rm -f "$TEMP"
fi
[Unit]
Description = update dns
[Service]
ExecStart = /home/nona/.dotfiles/bin/update-ddns.sh
Type = oneshot
User = nobody
[Install]
WantedBy = multi-user.target
[Unit]
Description=update dns
[Timer]
OnBootSec=5min
OnUnitActiveSec=1h
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment