Last active
August 29, 2015 14:02
-
-
Save tkuchiki/04467bfa38d2073bd80a to your computer and use it in GitHub Desktop.
aws ec2 describe-instances を叩いて [local ip] [hostname] を /etc/hosts に追記する
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 | |
# 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