Skip to content

Instantly share code, notes, and snippets.

@justindossey
Created November 13, 2024 16:27
Show Gist options
  • Save justindossey/5e3b78d01325501349f17fe75c0e446e to your computer and use it in GitHub Desktop.
Save justindossey/5e3b78d01325501349f17fe75c0e446e to your computer and use it in GitHub Desktop.
Check tailscale expiry on Linux and mac
#!/bin/bash
message=""
# what OS is this?
cur_os=$(uname -s)
# do we have jq?
if ! which jq >/dev/null 2>&1 ; then
echo "please install jq" >&2
exit 1
fi
case $cur_os in
Linux)
status=$(tailscale status --json |\
jq --arg tendays "$(date -d '10 day' +%s)" \
'.Self.KeyExpiry | fromdateiso8601 as $expiry | $expiry < ($tendays | tonumber)')
if [[ "$status" == "true" ]]; then
message="Tailscale key expiry is less than 10 days. Renew with 'tailscale up'"
fi
;;
Darwin)
# MacOS doesn't put the tailscale binary in PATH
tailscale='/Applications/Tailscale.app/Contents/MacOS/Tailscale'
# MacOS doesn't have GNU date, but 86400 * 10 = 864000. I don't care if we are
# a little off a couple of times a year due to Daylight Savings.
tendays=$(($(date +%s) + 864000))
status=$($tailscale status --json |\
jq --arg tendays "$tendays" \
'.Self.KeyExpiry | fromdateiso8601 as $expiry | $expiry < ($tendays | tonumber)')
if [[ "$status" == "true" ]]; then
message="Tailscale key expiry is less than 10 days. Renew soon!"
fi
;;
*)
message="Unknown OS; can't check Tailscale expiry"
;;
esac
if [[ -n "$message" ]] ; then
echo "$message" >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment