Skip to content

Instantly share code, notes, and snippets.

@bscott
Created December 19, 2023 17:24
Show Gist options
  • Save bscott/f4693f06e58245b7e17631ef87e300e0 to your computer and use it in GitHub Desktop.
Save bscott/f4693f06e58245b7e17631ef87e300e0 to your computer and use it in GitHub Desktop.
Tailscale unauthorized device cleanup
import requests
# Get API token from environment variable
api_token = os.getenv('TAILSCALE_API_TOKEN')
tailnet_name = os.getenv('TAILSCALE_TAILNET_NAME')
# see if we have an API token
if api_token is None:
print("Please set TAILSCALE_API_TOKEN environment variable")
exit(1)
if tailnet_name is None:
print("Please set TAILSCALE_TAILNET_NAME environment variable")
exit(1)
# Set headers
headers = {
'Authorization': f'Bearer {api_token}',
'Content-Type': 'application/json'
}
def get_machines_waiting_for_approval():
url = f'https://api.tailscale.com/api/v2/tailnet/{tailnet_name}/devices'
response = requests.get(url, headers=headers)
response.raise_for_status()
devices = response.json()['devices']
return [device for device in devices if device['authorized'] == False]
def delete_machine(machine_id):
url = f'https://api.tailscale.com/api/v2/device/{machine_id}'
response = requests.delete(url, headers=headers)
response.raise_for_status()
print(f"Deleted machine {machine_id}")
def main():
machines_to_delete = get_machines_waiting_for_approval()
for machine in machines_to_delete:
delete_machine(machine['id'])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment