Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Last active September 6, 2018 16:23
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 jaytaylor/42cb01d2244701863e293e7fea9cd30d to your computer and use it in GitHub Desktop.
Save jaytaylor/42cb01d2244701863e293e7fea9cd30d to your computer and use it in GitHub Desktop.
AWS Instance attributes recursive downloader
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Recursive AWS-style instance attributes downloader."""
import logging
import os
import re
import requests
log_formatter = logging.Formatter('%(asctime)s, %(levelname)s %(message)s')
logging.basicConfig(level=logging.INFO, formatter=log_formatter)
logger = logging.getLogger(__name__)
seed_url = 'http://169.254.169.254/latest'
def recurse(url):
logger.info('retrieving url=%s', url)
resp = requests.get(url)
if resp.status_code / 100 != 2:
logger.debug('url=%s returned non-2xx status code=%s (skipping)', url, resp.status_code)
return []
attr = re.sub(r'''http:\/\/[^\/]+\/''', '', url)
if not os.path.exists(attr):
os.makedirs(attr)
with open('%s.txt' % (attr,), 'w') as fh:
fh.write(resp.text)
new_urls = ['%s/%s' % (url, attr) for attr in resp.text.split('\n')] if resp.text else []
return new_urls
if __name__ == '__main__':
urls = [seed_url]
while len(urls):
urls += recurse(urls.pop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment