Skip to content

Instantly share code, notes, and snippets.

@nickstenning
Created February 28, 2017 12:22
Show Gist options
  • Save nickstenning/d50ed0e6a9327854d4cabe577a023567 to your computer and use it in GitHub Desktop.
Save nickstenning/d50ed0e6a9327854d4cabe577a023567 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -eu
if [ "$#" -lt "2" ]; then
echo >&2 "Usage: h-ssh <ENV> <APP> [SSH_ARGS]"
exit 1
fi
ENV=$1
APP=$2
shift 2
pickhost() {
local app=$1
local env=$2
aws ec2 describe-instances \
--filters "Name=tag:application,Values=${app}" \
"Name=tag:environment,Values=${env}" \
"Name=instance-state-name,Values=running" \
--query "Reservations[0].Instances[0].PublicDnsName" \
--output text
}
if ! HOSTNAME=$(pickhost "$APP" "$ENV"); then
echo >&2 "Error running awscli! Aborting."
exit 1
elif [ "$HOSTNAME" = "None" ]; then
echo >&2 "Couldn't find any matching hosts! Is that the right application name?"
exit 1
fi
ssh \
-l ec2-user \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeychecking=no \
"$HOSTNAME" \
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment