Skip to content

Instantly share code, notes, and snippets.

@mbabineau
Last active July 19, 2016 16:49
Show Gist options
  • Save mbabineau/319882 to your computer and use it in GitHub Desktop.
Save mbabineau/319882 to your computer and use it in GitHub Desktop.
# ssh-instance - function to let you SSH to an EC2 instance ID
# Written by Mike Babineau <michael.babineau@gmail.com>. Original here: https://gist.github.com/mbabineau/319882/
# Put this in ~/.profile and reload your terminal
function ssh-instance() {
if [ $# -lt 1 ] || [ $# -gt 3 ]; then
echo "Usage: ssh-instance [-r region] <instance id>"
else
case "$1" in
"-r")
if [ $# -eq 3 ] && [ -n "`echo $3|egrep \"^i-[0-9a-z]+\"`" ]; then
ssh `ec2-describe-instances --region $2 $3|grep "^INSTANCE"|cut -f4`
else
echo "Usage: ssh-instance [-r region] <instance id>"
return 1
fi;;
i-[0-9a-zA-Z]*)
if [ $# -eq 3 ] && [ "$2" == "-r" ]; then
ssh `ec2-describe-instances --region $3 $1|grep "^INSTANCE"|cut -f4`
elif [ $# -eq 1 ]; then
ssh `ec2-describe-instances $1|grep "^INSTANCE"|cut -f4`
else
echo "Usage: ssh-instance [-r region] <instance id>"
return 1
fi;;
*)
echo "Usage: ssh-instance [-r region] <instance id>"
esac
fi
return 0
}
#!/bin/bash
# ssh-instance - script to let you SSH to an EC2 instance ID
# Written by Mike Babineau <michael.babineau@gmail.com>. Original here: https://gist.github.com/mbabineau/319882/
if [ $# -lt 1 ] || [ $# -gt 3 ]; then
echo "Usage: ssh-instance [-r region] <instance id>"
else
case "$1" in
"-r")
if [ $# -eq 3 ] && [ -n "`echo $3|egrep \"^i-[0-9a-z]+\"`" ]; then
ssh `ec2-describe-instances --region $2 $3|grep "^INSTANCE"|cut -f4`
else
echo "Usage: ssh-instance [-r region] <instance id>"
exit 1
fi;;
i-[0-9a-zA-Z]*)
if [ $# -eq 3 ] && [ "$2" == "-r" ]; then
ssh `ec2-describe-instances --region $3 $1|grep "^INSTANCE"|cut -f4`
elif [ $# -eq 1 ]; then
ssh `ec2-describe-instances $1|grep "^INSTANCE"|cut -f4`
else
echo "Usage: ssh-instance [-r region] <instance id>"
exit 1
fi;;
*)
echo "Usage: ssh-instance [-r region] <instance id>"
esac
fi
exit 0
@sebykrueger
Copy link

Nice little script. I have fixed it to work with the new aws cli.


function ssh-instance() {
    if [ $# -lt 1 ] || [ $# -gt 3 ]; then
        echo "Usage: ssh-instance [-r region] "
    else
        case "$1" in
            "-r")
                if [ $# -eq 3 ] && [ -n "`echo $3|egrep \"^i-[0-9a-z]+\"`" ]; then
                    ssh `aws ec2 describe-instances --region $2 --instance-ids $3 --output text --query Reservations[0].Instances[0].PrivateIpAddress`
                else
                    echo "Usage: ssh-instance [-r region] "
                    return 1
                fi;;
            i-[0-9a-zA-Z]*)
                if [ $# -eq 3 ] && [ "$2" == "-r" ]; then
                    ssh `aws ec2 describe-instances --region $3 --instance-ids $1 --output text --query Reservations[0].Instances[0].PrivateIpAddress`
                elif [ $# -eq 1 ]; then
                    ssh `aws ec2 describe-instances --instance-ids $1 --output text --query Reservations[0].Instances[0].PrivateIpAddress`
                else
                    echo "Usage: ssh-instance [-r region] "
                    return 1
                fi;;
            *)
                echo "Usage: ssh-instance [-r region] "
            esac
    fi

    return 0
}

@oillio
Copy link

oillio commented Jul 19, 2016

Thanks @sebykrueger.
FYI - I had to add quotes (") around the query text to get this to work.
My final script:

# ssh-instance - function to let you SSH to an EC2 instance ID
# Written by Mike Babineau <michael.babineau@gmail.com>. Original here: https://gist.github.com/mbabineau/319882/
# Put this in ~/.profile and reload your terminal
function ssh-instance() {
    if [ $# -lt 1 ] || [ $# -gt 3 ]; then
        echo "Usage: ssh-instance [-r region] <instance id>"
    else
        case "$1" in
            "-r")
                if [ $# -eq 3 ] && [ -n "`echo $3|egrep \"^i-[0-9a-z]+\"`" ]; then
                    ssh `aws ec2 describe-instances --region $2 --instance-ids $3 --output text --query "Reservations[0].Instances[0].PrivateIpAddress"`
                else
                    echo "Usage: ssh-instance [-r region] <instance id>"
                    return 1
                fi;;
            i-[0-9a-zA-Z]*)
                if [ $# -eq 3 ] && [ "$2" == "-r" ]; then
                    ssh `aws ec2 describe-instances --region $3 --instance-ids $1 --output text --query "Reservations[0].Instances[0].PrivateIpAddress"`
                elif [ $# -eq 1 ]; then
                    ssh `aws ec2 describe-instances --instance-ids $1 --output text --query "Reservations[0].Instances[0].PrivateIpAddress"`
                else
                    echo "Usage: ssh-instance [-r region] <instance id>"
                    return 1
                fi;;
            *)
                echo "Usage: ssh-instance [-r region] <instance id>"
            esac
    fi

    return 0
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment