Skip to content

Instantly share code, notes, and snippets.

@totomz
Created November 17, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totomz/b2f5933946e25d28396c9264ac9ed366 to your computer and use it in GitHub Desktop.
Save totomz/b2f5933946e25d28396c9264ac9ed366 to your computer and use it in GitHub Desktop.
Cleanup stale nodes from ZeroTier
import requests
import datetime
import od
URI = "https://my.zerotier.com/api"
ZT_TOKEN = os['ZT_TOKEN']
ZT_NETWORK = os['ZT_NETWORK']
with requests.Session() as s:
s.headers.update({'Authorization': f"bearer {ZT_TOKEN}",
'Content-Type': 'application/json'})
r = s.get(f"{URI}/network/{ZT_NETWORK}/member")
members = r.json()
print(f"There are {len(members)} members")
i = 0
for m in members:
id = m['id'].split('-')[1]
print(f"Node: {id}")
if m['online'] is True:
print(" --> Online, skipping")
continue
i += 1
if m['lastOnline'] == 0:
print(" !! Offline since forever, ciaone!")
s.delete(f"{URI}/network/{ZT_NETWORK}/member/{id}")
else:
d1 = datetime.datetime.fromtimestamp(m['lastOnline']/1000)
diff = (datetime.datetime.now() - d1)
if diff.days < 2:
print("!! !!! Seen few days ago - skipping")
else:
s.delete(f"{URI}/network/{ZT_NETWORK}/member/{id}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment