Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bin/ec2-ssh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

usage() {
cat<<EOF
Usage: ec2-ssh [-k KEY] [-s SECRET] [-r REGION] [-t TAG] <instance-name>
Usage: ec2-ssh [-k KEY] [-s SECRET] [-r REGION] [-t TAG] [-p PORT] <instance-name>

Open an ssh connection to an EC2 instance where <instance-name>=tag:value.
The 'tag:' portion of <instance-name> is optional, and defaults to 'Name'.
Expand All @@ -15,6 +15,7 @@ For a list of instances, run ec2-host without any paramteres
-s, SECRET Amazon EC2 Secret, defaults to \$AWS_SECRET_ACCESS_KEY
-r, REGION Amazon EC2 Region, defaults to us-east-1
-t, TAG Tag name for searching, defaults to 'Name'
-p, PORT Set SSH port value (default 22)
EOF
}

Expand All @@ -23,13 +24,15 @@ test $# -eq 0 && { usage; exit; }

# Process options
cmd="ec2-host"
while getopts ":hk:s:r:t:" opt; do
port=22
while getopts ":hk:s:r:t:p:" opt; do
case $opt in
h ) usage; exit 1;;
k ) cmd="$cmd -k $OPTARG";;
s ) cmd="$cmd -s $OPTARG";;
r ) cmd="$cmd -r $OPTARG";;
t ) cmd="$cmd -t $OPTARG";;
p ) port="$OPTARG";;
\? ) usage; exit 1
esac
done
Expand Down Expand Up @@ -67,4 +70,4 @@ cmd="echo \\\". ~/.bashrc && PS1='\[\e]0;$inst: \w\\\a\]\[\\\033[01;32m\]$inst\[
if test "${@:2}"; then
cmd="${@:2}"
fi
test -n "$host" && echo "Connecting to $host." && exec sh -c "ssh -t $user@$host \"$cmd\""
test -n "$host" && echo "Connecting to $host:$port." && exec sh -c "ssh -t -p $port $user@$host \"$cmd\""