Created
August 7, 2020 20:55
-
-
Save jbylund/bed030929ea3bbeef6e94e1f130f606c to your computer and use it in GitHub Desktop.
get aws metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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