shortssh: a SSH Shortcut with Bash

Hi there! Lately, I came into a scenario where I need to create and delete a lot of AWS EC2 instances. This constant building and tearing down gives a lot of different IP addresses for the instances I want to access every time. In addition to that, these instances have different key files depending on what project I built the instances for.

For SSH-ing to the instances, I already have this usual template:

ssh -i ~/Downloads/adelen-key.pem -o "StrictHostKeyChecking no" <ip-address>

Surely, doing a reverse search for the last occurrence of this command from the command line is easy but lately, since I maintain multiple key files, I have to edit whatever the last command was to include the appropriate key file name and IP address. Because of this seemingly tedious process, I was inspired to do a simple bash script to serve as a shortcut to the ssh command above.


#!/bin/bash

if [[ $1 &amp;amp;&amp;amp; $2 ]]; then

  KEY_DIRECTORY=`printenv SHORTSSH_KEY_DIRECTORY`

  if [[ $KEY_DIRECTORY ]]; then

    if [ -d $KEY_DIRECTORY ]; then

      KEY_FILE="$KEY_DIRECTORY/$1.pem"

      if [ -f $KEY_FILE ]; then
        ssh -i $KEY_FILE -o "StrictHostKeyChecking no" $2
      else
        echo "[+] File $1.pem not found in $KEY_DIRECTORY"
      fi
    else
     echo "[+] Cannot find directory: $KEY_DIRECTORY"
    fi
   else
    echo "[+] Set the location of your identity key file as    SHORTSSH_KEY_DIRECTORY in your environment variables"
 fi
else
 echo "SAMPLE USAGE: shortssh key-file ubuntu@123.456.78.90"
fi

This is very simple but serves the purpose I was needing a solution for. To connect to an EC2 instance we can just issue the following command:

shortssh filename-of-key user-name@ip-address

So for example, we want to access our server at IP address 54.192.84.33:

shortssh adelen-key ubuntu@54.192.84.33

I also thought of having the ubuntu@ hard coded in the script so we could just input the IP address but I decided not to so that this can be also useful with other servers/EC2 instances not running Ubuntu (i.e. Amazon Linux with ec2-user@ip-address).

If you wish to use this, just follow the following steps:

  1. Download the file. Either copy the one from above or download the script from my Github Repository. 🙂
  2. Set SHORTSSH_KEY_DIRECTORY as part of your environment variables. Set its value to the location of your key files. (I have mine at /Users/apfestin/Keys)
  3. Add the directory where the shortssh file is to your PATH variable so that you can issue the shortssh command from anywhere in your file system

To accomplish the steps above, I added the following lines to my ~/.profile:

export PATH=$PATH:/Users/apfestin/Desktop/short-scripts
export SHORTSSH_KEY_DIRECTORY=/Users/apfestin/Keys

So there, a shortcut to my most commonly used SSH command. Hope you could find this useful! 🙂

Thanks for reading! 🙂

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s