Skip to content

Instantly share code, notes, and snippets.

@toddkaufmann
Created August 23, 2024 00:07
Show Gist options
  • Save toddkaufmann/78934ffced82d779fddde03a0d3874d1 to your computer and use it in GitHub Desktop.
Save toddkaufmann/78934ffced82d779fddde03a0d3874d1 to your computer and use it in GitHub Desktop.
Zerotier client list, without going to the web
#!/bin/bash
json="$1" # a temp file to reuse/save members (or debug/iterate)
if [[ -z "$json" ]]; then
json=/tmp/$(mktemp zt.XXXXXX).json
fi
set -eu -o pipefail
if ! [ -f "$json" ]; then
# ZT_NETWORK_ID="2468abcd13579ef0" # your network
# ZT_KEY='ShhhhDontTellAnyoneItsAsekretxyz' # your API key
curl -s -H "Authorization: Bearer $ZT_KEY" https://my.zerotier.com/api/network/$ZT_NETWORK_ID/member > $json
fi
DATE=$(which date)
# if your 'date' is not gnu date then set path here (mac: brew info coreutils)
if [ -f /usr/local/bin/gdate ]; then DATE=/usr/local/bin/gdate; fi
# 2023-04-06 'online' field no longer exists; use lastSeen for active hosts
# since_when=$(( $($DATE --date="yesterday" +%s) * 1000 ))
# since_when=$(( $($DATE -u --date="3 minutes ago" +%s) * 1000 ))
since_when=$(( $($DATE -u --date="33 minutes ago" +%s) * 1000 ))
if ! jq -r ".[] | select(.lastSeen > $since_when )"' | [ .physicalAddress, .config.ipAssignments[0], .name, .description ] | join(" ")' < $json | sort; then
echo SOMETHING FAIL $? >&2
exit 1
fi
exit 0
# Run like this (or set env.vars in shell, or script):
# Output is public IP, ZT IP, name, and description
#
# $ ZT_NETWORK_ID="2468abcd13579ef0" ZT_KEY=ShhhhDontTellAnyoneItsAsekretxyz ztlist-gist.sh
# 100.17.40.54 10.99.88.33 Todd Todd Desktop
# 12.42.165.2 10.99.88.156 trs82-armv8-01 trs82-armv8-01 auto-authorized 2024-03-05.0518
# 109.119.173.34 10.99.88.178 trs81-cray-1 trs81-cray-1 auto-authorized 2023-11-22.2341
# 109.119.173.34 10.99.88.194 trs81-armv8-05 Changed to user=adminx, trs81-armv8-05 at 2023-11-29.0858
# 63.113.215.228 10.99.88.87 trs86-armv8-01 trs86-armv8-01 auto-authorized 2024-07-31.1741
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment