Skip to content

Instantly share code, notes, and snippets.

@murarisumit
Forked from lavie/ec2hosts.py
Last active August 29, 2015 14:18
Show Gist options
  • Save murarisumit/15d7f8d8e22361e0e2d6 to your computer and use it in GitHub Desktop.
Save murarisumit/15d7f8d8e22361e0e2d6 to your computer and use it in GitHub Desktop.
Get only running instance from a region and their IP Address
from boto.ec2 import *
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("access_key", help = "AWS Access Key")
parser.add_argument("secret_key", help = "AWS Secret Key")
parser.add_argument("--region", help = "AWS Region", default = "us-east-1")
parser.add_argument("--all", help = "show not just running instances", action = "store_true")
args = parser.parse_args()
conn = connect_to_region(args.region, aws_access_key_id = args.access_key, aws_secret_access_key = args.secret_key)
instances = conn.get_only_instances()
filters = { }
if not args.all:
filters = {
'instance-state-name' : 'running'
}
instances = conn.get_only_instances(filters = filters)
for instance in instances:
print instance.ip_address + " " + instance.tags['Name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment