Last active
October 11, 2019 18:17
-
-
Save SolomonHD/30060471804df63e32c1f0a23251d2e3 to your computer and use it in GitHub Desktop.
Bash function for SSH into EC2 by Name Tag
This file contains hidden or 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
<< //// | |
This function will ssh into a EC2 based on Name tags. You can ssh in via public or private IP This function requires awscli to work | |
There are several optional environment variables: | |
AWS_PROFILE = Control which profile is active with this variable, if this is unset the function will use AWS_DEFAULT_PROFILE instead | |
SSH_EC2_KEY_FILE = path to the key file for the user | |
SSH_EC2_USER = name of the user that logs in, if this variable is unset the value is ec2-user | |
To use this function do the following commands | |
(from command line) | |
1) mkdir -p ~/gist | |
2) git -C ~/gist clone [email protected]:30060471804df63e32c1f0a23251d2e3.git | |
Then copy the following (if to fi) into your ~/.bashrc | |
if [ -f $HOME/gist/30060471804df63e32c1f0a23251d2e3/ssh-to-ec2-by-tag-name ]; then | |
. $HOME/gist/30060471804df63e32c1f0a23251d2e3/ssh-to-ec2-by-tag-name | |
fi | |
Then either 'source ~/.bashrc' or close the terminal and open a new one | |
//// | |
SSH_EC2_PARAMS='-o ServerAliveInterval=30 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ForwardAgent=yes' | |
function hostname_from_instance() { | |
if [ ! -z "$AWS_PROFILE" ]; then | |
AWS_DEFAULT_PROFILE=$AWS_PROFILE; | |
fi | |
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" "{\"Name\":\"instance-state-name\", \"Values\":[\"running\", \"pending\"]}" --query='Reservations[0].Instances[0].PublicDnsName' | tr -d '"'); | |
} | |
function ip_from_instance_priv() { | |
if [ ! -z "$AWS_PROFILE" ]; then | |
AWS_DEFAULT_PROFILE=$AWS_PROFILE; | |
fi | |
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" "{\"Name\":\"instance-state-name\", \"Values\":[\"running\", \"pending\"]}" --query='Reservations[0].Instances[0].PrivateIpAddress' | tr -d '"'); | |
} | |
function ip_from_instance_pub() { | |
if [ ! -z "$AWS_PROFILE" ]; then | |
AWS_DEFAULT_PROFILE=$AWS_PROFILE; | |
fi | |
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" "{\"Name\":\"instance-state-name\", \"Values\":[\"running\", \"pending\"]}" --query='Reservations[0].Instances[0].PublicIpAddress' | tr -d '"'); | |
} | |
function ssh-ec2-priv() { | |
if [ -z "$SSH_EC2_USER" ]; then | |
SSH_EC2_USER='ec2-user'; | |
fi | |
if [ -z "$SSH_EC2_KEY_FILE" ]; then | |
ssh $SSH_EC2_PARAMS $SSH_EC2_USER@$(ip_from_instance_priv "$1"); | |
else | |
ssh $SSH_EC2_PARAMS -i $SSH_EC2_KEY_FILE $SSH_EC2_USER@$(ip_from_instance_priv "$1"); | |
fi | |
} | |
function ssh-ec2-pub() { | |
if [ -z "$SSH_EC2_USER" ]; then | |
SSH_EC2_USER='ec2-user'; | |
fi | |
if [ -z "$SSH_EC2_KEY_FILE" ]; then | |
ssh $SSH_EC2_PARAMS $SSH_EC2_USER@$(ip_from_instance_pub "$1"); | |
else | |
ssh $SSH_EC2_PARAMS -i $SSH_EC2_KEY_FILE $SSH_EC2_USER@$(ip_from_instance_pub "$1"); | |
fi | |
} | |
function sftp-ec2-priv() { | |
if [ -z "$SSH_EC2_USER" ]; then | |
SSH_EC2_USER='ec2-user'; | |
fi | |
if [ -z "$SSH_EC2_KEY_FILE" ]; then | |
sftp $SSH_EC2_PARAMS $SSH_EC2_USER@$(ip_from_instance_priv "$1"); | |
else | |
sftp $SSH_EC2_PARAMS -i $SSH_EC2_KEY_FILE $SSH_EC2_USER@$(ip_from_instance_priv "$1"); | |
fi | |
} | |
function sftp-ec2-pub() { | |
if [ -z "$SSH_EC2_USER" ]; then | |
SSH_EC2_USER='ec2-user'; | |
fi | |
if [ -z "$SSH_EC2_KEY_FILE" ]; then | |
sftp $SSH_EC2_PARAMS $SSH_EC2_USER@$(ip_from_instance_pub "$1"); | |
else | |
sftp $SSH_EC2_PARAMS -i $SSH_EC2_KEY_FILE $SSH_EC2_USER@$(ip_from_instance_pub "$1"); | |
fi | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment