Skip to content

Instantly share code, notes, and snippets.

@nna774 nna774/update-ddns.sh
Last active May 1, 2019

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