Skip to content

Instantly share code, notes, and snippets.

@jnahelou
Last active May 12, 2022 16:08
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 jnahelou/94edfc8d79757af8e5f162ca9cb8f808 to your computer and use it in GitHub Desktop.
Save jnahelou/94edfc8d79757af8e5f162ca9cb8f808 to your computer and use it in GitHub Desktop.
List users with ssh keys
import googleapiclient.discovery
compute_v1 = googleapiclient.discovery.build(
"compute", "v1", cache_discovery=False
)
project_id = "<project_id>"
project_info = compute_v1.projects().get(project=project_id).execute()
for metadata in project_info["commonInstanceMetadata"].get("items", []):
if metadata["key"] == "ssh-keys":
users = []
for google_key in metadata["value"].split('\n'):
# Add expireOn exclusion ?
users.append( google_key.split(':')[0])
print('{},{}'.format(project_id, ';'.join(users)))
google-api-python-client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment