Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Last active August 29, 2015 14:02
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 tkuchiki/04467bfa38d2073bd80a to your computer and use it in GitHub Desktop.
Save tkuchiki/04467bfa38d2073bd80a to your computer and use it in GitHub Desktop.
aws ec2 describe-instances を叩いて [local ip] [hostname] を /etc/hosts に追記する
#!/usr/bin/env python
# coding: utf-8
import sys
import json
import subprocess
p = subprocess.Popen(['aws', 'ec2', 'describe-instances'], shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
j = json.loads('\n'.join(p.stdout.readlines()))
fp = open("/etc/hosts", "a")
for r in j.get('Reservations'):
for i in r.get('Instances'):
for tag in i.get('Tags'):
if tag["Key"] == "Name":
fp.write("%s %s\n" % (i.get('PrivateIpAddress'), tag["Value"]))
break
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment