Created
January 6, 2011 02:23
-
-
Save shayne/767404 to your computer and use it in GitHub Desktop.
Easily get ec2 public hosts, open a shell or run commands...
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 os | |
import sys | |
import getopt | |
import boto | |
from sys import stderr | |
_ec2 = None | |
def short_usage(): | |
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [NAME] | |
ec2-host django8 => ec2-53-19-113-121.compute-1.amazonaws.com | |
Try `ssh-distillery --help' for more information.""" | |
def full_usage(): | |
print >>stderr, """Usage: ec2-host [-k KEY] [-s SECRET] [NAME] | |
Prints server host name. | |
--help display this help and exit | |
-k, --aws-key KEY Amazon EC2 Key, defaults to ENV[AWS_KEY] | |
-s, --aws-secret SECRET Amazon EC2 Secret, defaults to ENV[AWS_SECRET]""" | |
def list_instances(): | |
instance_pairs = [] | |
for instance in ec2_instances(): | |
instance_name = instance.tags.get("Name") | |
if instance.public_dns_name: | |
pair = (instance_name, instance.public_dns_name) | |
instance_pairs.append(pair) | |
for pair in sorted(instance_pairs, key=lambda p: p[0]): | |
print "%s\t%s" % pair | |
def print_host(name): | |
filters = {'tag:Name': name} | |
instances = ec2_instances(filters=filters) | |
if len(instances) == 0: | |
print >>stderr, 'Unable to match instance named "%s"' % name | |
short_usage() | |
sys.exit(1) | |
print instances[0].public_dns_name | |
def ec2_instances(**kwargs): | |
return [rsv.instances[0] for rsv in _ec2.get_all_instances(**kwargs)] | |
def main(argv): | |
try: | |
opts, args = getopt.getopt(argv, "hLk:s:", | |
["help", "aws-key", "aws-secret"]) | |
except getopt.GetoptError, err: | |
print >>sys.stderr, err | |
short_usage() | |
sys.exit(2) | |
aws_key = os.environ.get("AWS_KEY") | |
aws_secret = os.environ.get("AWS_SECRET") | |
for opt, arg in opts: | |
if opt in ("-h", "--help"): | |
full_usage() | |
sys.exit() | |
elif opt in("-k", "--aws-key"): | |
aws_key = arg | |
elif opt in("-s", "--aws-secret"): | |
aws_secret = arg | |
if not aws_key or not aws_secret: | |
if not aws_key: | |
print >>sys.stderr,\ | |
"AWS_KEY not set in environment and not",\ | |
"specified by --aws-key KEY or -k KEY" | |
if not aws_secret: | |
print >>sys.stderr,\ | |
"AWS_SECRET not set in environment and not",\ | |
"specified by --aws-secret SECRET or -s SECRET" | |
short_usage() | |
sys.exit(2) | |
global _ec2 | |
_ec2 = boto.connect_ec2(aws_key, aws_secret) | |
argc = len(args) | |
if argc == 0: | |
list_instances() | |
sys.exit() | |
else: | |
if argc > 1: | |
print >>stderr, "Warning: more than one name given" | |
name = args[0] | |
print_host(name) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
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
#!/bin/sh | |
#/ Usage: ec2-ssh <instance-name> | |
#/ Open ssh connection to EC2 instance where tag:Name=<instance-name> | |
#/ For list of instance, run ec2-host without any paramteres | |
set -e | |
test $# -eq 0 -o $(expr "$*" : ".*--help") \ | |
-ne 0 -o $(expr "$*" : ".*-h") -ne 0 && { | |
grep ^#/ < $0 | | |
cut -c4- | |
exit | |
} | |
# support user@instance-name format | |
IFS="@"; declare -a hostparts=($1) | |
inst="${hostparts[1]}" | |
user="${hostparts[0]}" | |
if test -z "$inst"; then | |
inst="$1" | |
user="ubuntu" | |
fi | |
# get host from ec2-host command | |
host=$(ec2-host $inst) | |
# pass all other parameters (${@:2}) to ssh allowing | |
# things like: ec2-ssh nginx uptime | |
test -n "$host" && exec sh -c "ssh $user@$host ${@:2}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment