Skip to content

Instantly share code, notes, and snippets.

@benkehoe
Last active January 31, 2017 21:59
Show Gist options
  • Save benkehoe/55d8c9d0de7a30a72537 to your computer and use it in GitHub Desktop.
Save benkehoe/55d8c9d0de7a30a72537 to your computer and use it in GitHub Desktop.
AWS EC2 ssh shell functions
# Shell functions for ssh-ing into and
# Features automatic adding of the key file when it is specified in
# AWSSH_KEY. If no user is given in the hostname, it will use the value
# in AWSSH_DEFAULT_USER, or ec2-user if that is not set.
# Sets StrictHostKeyChecking off, so it doesn't ask you if you want to
# connect. Doesn't add the IP to known_hosts, though it will warn you
# every time that it has.
awssh ()
(
# Config:
# AWSSH_KEY
# AWSSH_DEFAULT_USER (ec2-user if not set)
AWSSH_KEY_OPT=
if [ -n "$AWSSH_KEY" ]; then
AWSSH_KEY_OPT="-i $AWSSH_KEY"
fi;
if [ -z "$AWSSH_DEFAULT_USER" ]; then
AWSSH_DEFAULT_USER=ec2-user
fi
AWSSH_USER_OPT=
if ! echo "$1" | $(which grep) -q @; then
AWSSH_USER_OPT="-o User=$AWSSH_DEFAULT_USER"
fi;
ssh $AWSSH_KEY_OPT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $AWSSH_USER_OPT "$@"
)
awscp ()
(
# Config:
# AWSSH_KEY
# AWSSH_DEFAULT_USER (ec2-user if not set)
AWSSH_KEY_OPT=
if [ -n "$AWSSH_KEY" ]; then
AWSSH_KEY_OPT="-i $AWSSH_KEY"
fi;
if [ -z "$AWSSH_DEFAULT_USER" ]; then
AWSSH_DEFAULT_USER=ec2-user
fi
AWSSH_USER_OPT="-o User=$AWSSH_DEFAULT_USER"
for var in "$@"
do
if echo "$var" | $(which grep) -q @; then
AWSSH_USER_OPT=
break
fi
done
scp $AWSSH_KEY_OPT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $AWSSH_USER_OPT "$@"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment