Created
April 15, 2022 10:21
-
-
Save yunazuno/eff90778833683212ac7c60dac78ec90 to your computer and use it in GitHub Desktop.
Delete all devices on the Tailnet except for no expiry devices
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
#!/bin/bash | |
# Delete all devices on the Tailnet except for no expiry devices | |
# Released under the MIT license | |
set -eo pipefail | |
TAILSCALE_TAILNET=${TAILSCALE_TAILNET:-} | |
TAILSCALE_API_KEY=${TAILSCALE_API_KEY:-} | |
SCRIPT_REAL_RUN=${SCRIPT_REAL_RUN:false} | |
if [ -z "${TAILSCALE_API_KEY}" ]; then | |
echo "[ERROR] TAILSCALE_API_KEY is not set" | |
exit 1 | |
fi | |
if [ -z "${TAILSCALE_TAILNET}" ]; then | |
echo "[ERROR] TAILSCALE_TAILNET is not set" | |
exit 1 | |
fi | |
if [ "${SCRIPT_REAL_RUN}" != "true" ]; then | |
echo "[INFO] DRY RUN" | |
fi | |
devices=$(curl -s -u "${TAILSCALE_API_KEY}:" "https://api.tailscale.com/api/v2/tailnet/${TAILSCALE_TAILNET}/devices" | jq -r '.devices[] | select(.keyExpiryDisabled == false) | .id') | |
for device_id in $devices; do | |
echo "[INFO] Deleting" $device_id | |
if [ "${SCRIPT_REAL_RUN}" = "true" ]; then | |
curl -X DELETE -s -u "${TAILSCALE_API_KEY}:" "https://api.tailscale.com/api/v2/device/${device_id}" | |
sleep 1 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment