Last active
February 4, 2023 20:02
-
-
Save kulp/eb105c1a5e15d7c32f67b2b4fba0bcd4 to your computer and use it in GitHub Desktop.
Check public IP address and update NearlyFreeSpeech DNS with the result
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
#!/usr/bin/env bash | |
curl --silent --fail -4 api64.ipify.org "$@" || | |
curl --silent --fail https://httpbin.org/ip "$@" | jq --raw-output .origin |
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
#!/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 |
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.