Skip to content

Instantly share code, notes, and snippets.

@kulp
Last active February 4, 2023 20:02
Show Gist options
  • Save kulp/eb105c1a5e15d7c32f67b2b4fba0bcd4 to your computer and use it in GitHub Desktop.
Save kulp/eb105c1a5e15d7c32f67b2b4fba0bcd4 to your computer and use it in GitHub Desktop.
Check public IP address and update NearlyFreeSpeech DNS with the result
#!/usr/bin/env bash
curl --silent --fail -4 api64.ipify.org "$@" ||
curl --silent --fail https://httpbin.org/ip "$@" | jq --raw-output .origin
#!/usr/bin/env bash
# Fail early and loudly.
set -euo pipefail
here=$(dirname $0)
store=$HOME/.public-ip
if [[ "$(cat $store || true)" != "$($here/get_ip.sh | tee $store)" ]]
then
exec "$@"
fi
#!/usr/bin/env bash
# Fail early and loudly.
set -euo pipefail
sha1 ()
{
echo -n "$1" | shasum | cut -b1-40
}
user=$1
api_key=$2
fqdn=$3
ip=$4
shift 4
domain=${fqdn#*.}
subdomain=${fqdn%%.*}
ttl=600
now=$(date +%s)
salt=$( (LC_ALL=C tr -c -d A-Za-z0-9 < /dev/urandom || true) | dd bs=1 count=16 2> /dev/null)
base=https://api.nearlyfreespeech.net
request_uri=/dns/$domain/replaceRR
# TODO support IPv6 too.
body="name=$subdomain&type=A&data=$ip&ttl=$ttl"
body_hash=$(sha1 "$body")
hash="$(sha1 "$user;$now;$salt;$api_key;$request_uri;$body_hash")"
auth="$user;$now;$salt;$hash"
echo -n "$body" |
curl --silent \
--fail \
--data @- \
--header "X-NFSN-Authentication: $auth" \
$base$request_uri
@kulp
Copy link
Author

kulp commented Jan 23, 2022

This Bash script can be used to update a DNS type A (IPv4) record on a domain hosted with NearlyFreeSpeech. This can be useful for keeping a DNS entry updated for a home IP address.

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