Skip to content

Instantly share code, notes, and snippets.

@jbylund
Created August 7, 2020 20:55
Show Gist options
  • Save jbylund/bed030929ea3bbeef6e94e1f130f606c to your computer and use it in GitHub Desktop.
Save jbylund/bed030929ea3bbeef6e94e1f130f606c to your computer and use it in GitHub Desktop.
get aws metadata
#!/usr/bin/env python
import requests
import json
def crawl(iurl):
results = [iurl.rstrip("/") + "/" + x for x in requests.get(iurl).text.split("\n")]
for endpoint in (x for x in results if not x.endswith("/")):
ept = requests.get(endpoint).text
if ept and ept[0] in '{[':
ept = json.loads(ept)
yield endpoint, ept
for endpoint in (x for x in results if x.endswith("/")):
for res in crawl(endpoint):
yield res
def get_metadata():
metadata = {}
for iurl, result in crawl("http://169.254.169.254/latest/meta-data"):
path = iurl.split("/")[5:]
ptr = metadata
for inode in path[:-1]:
ptr.setdefault(inode, {})
ptr = ptr[inode]
ptr[path[-1]] = result
print(json.dumps(metadata, indent=4, sort_keys=True))
def main():
get_metadata()
if "__main__" == __name__:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment