Skip to content

Instantly share code, notes, and snippets.

@un1tz3r0
Created August 31, 2019 16:02
Show Gist options
  • Select an option

  • Save un1tz3r0/70ce39ef6771d11374e458cce8e42d6a to your computer and use it in GitHub Desktop.

Select an option

Save un1tz3r0/70ce39ef6771d11374e458cce8e42d6a to your computer and use it in GitHub Desktop.
Script for use as sshd authorized_keys command on DigitalOcean droplets. It pulls the list of public ssh keys from your account profile using DigitalOcean's API, so that you can manage your authorized_keys in one convenient place.
#!/usr/bin/python3
import requests
import json
import os
def get(path):
url = "https://api.digitalocean.com/v2/{}".format(path)
apikey = os.environ['DIGITALOCEAN_API_KEY']
headers = {
"Authorization": "Bearer {}".format(apikey),
"Content-Type": "application/json"
}
r = requests.get(url, headers=headers)
return json.loads(r.content)
def accountkeys():
return get("account/keys")
if __name__ == "__main__":
import os
try:
assert "DIGITALOCEAN_API_KEY" in os.environ.keys()
except:
raise RuntimeError("Please ensure that the DIGITALOCEAN_API_KEY environment variable is set to you API secret token")
keys = accountkeys()
print("\n".join([v['public_key'] for v in keys['ssh_keys']]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment