Create a gist now

Instantly share code, notes, and snippets.

@migrs /ec2-ssh
Created Mar 14, 2013

What would you like to do?
#!/bin/sh
set -e
ssh='ssh'
ec2din="ec2-describe-instances"
while [ $# -gt 0 ]; do
case $1 in
-*)
if [ -z "$2" ]; then
break
elif [ "${2:0:1}" == "-" ]; then
args="$args $1"
shift
else
args="$args $1 $2"
shift 2
fi
;;
*)
if [ -n "$1" -a -z "$name" ]; then
name=$1
shift
fi
break
esac
done
if [ -n "$name" ]; then
if [ "$name" != "${name%\#*}" ]; then
num=${name#*\#}
name=${name%\#*}
fi
[ -z "$num" ] && num=1
if [ "$name" != "${name%@*}" ]; then
user=${name%@*}
name=${name#*@}
fi
q="-F \"tag:Name=$name\""
host=`$ec2din $q --hide-tags | grep ^INSTANCE | cut -f4 | paste -s | cut -f$num`
if [ -n "$host" ]; then
echo $host >&2
else
echo ec2-ssh: Could not find ec2 instance $name [$num] >&2
exit 1
fi
fi
if [ -n "$user" ]; then
exec $ssh $args $user@$host $@
else
exec $ssh $args $host $@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment