SSH Script for Auto-Login

Interactive SSH Auto-Logins

When running multiple Unix-like web servers, it can be a pain to connect to them via SSH when it comes domain names, private keys paths, IPs, and even port numbers. Now I’m sure there are server management tools for the big boys, but this is a simple script that runs on OS X (and with some modification even a Linux distro 🙂).

In this example, I will use hard-coded IP addresses which I prefer, but you wholesale nba jerseys can use domain names if you wish. Also, I’ve changed my SSH port to something other than the default 22. If you know a bit a bash scripting, then you can modify this to your needs.

Below is the entire code, which I will explain later on:

#!/bin/bash


#  Boobs  This script will ask which websites you
# would like to SSH into automatically

# IP Definitions for easier modification
# Assign IPs to variables (no spaces or dashes allowed):
HEAPGEEK=126.236.152.43
KALFUS=192.251.196.219
VPN=104.216.97.52
TESTSERVER=192.241.201.197

# Print the directions to the end user
clear
echo --- SSH Logins ----
echo Please select number from list below:
echo 1 heapgeek.com
echo 2 kalfus.org
echo 3 VPN
echo 4 TESTINGSERVER

# Read the user input
read option
if [ "$option" = "1" ]
   wholesale mlb jerseys    then
        echo You have selected option 1
        echo Loading . . .
        sleep 2
        ssh -p 2022 -i ~/.ssh/digitalOcean-VM joseph@$HEAPGEEK
elif [ "$option" = "2" ]
    then
        echo You have selected option 2
        echo Loading . . .
        sleep 2
        ssh -p 2022 -i ~/.ssh/digitalOcean-VM joseph@$KALFUS
elif [ "$option" = "3" ]
    then
        echo You have selected option 3
        echo Loading . . .
        sleep 2
        ssh -p 2022 -i ~/.ssh/digitalOcean-VM joseph@$VPN
elif [  Network  "$option" = "4" ]
    then
        echo You have selected option 4
        echo Loading . . .
        sleep 2
        ssh -p 2022 -i ~/.ssh/testing-server-cert joseph@$TESTSERVER
else
    echo Invalid Option
fi
sleep 3

Explanation

The first part of the script defines the IP addresses and variable names so we can reference them later. If you’re like me and prefer IP addresses over domain town" names then here is where you would change them if needed. For example, my VPN doesn’t have a domain name associated with it, so a IP works in this case.

The next part just displays the list of options for the user to choose from. You can add and remove these as needed.

The next part reads the user input and executes the appropriate if-block. I’ve put in several sleep commands to delay it a bit.

The main line that needs to be modified would be:

ssh -p 2022 -i ~/.ssh/testing-server-cert joseph@$TESTSERVER

where:

ssh is the actual Secure Shell command
-p 2022 is the port number (doesn’t need to be specified if defaulted to 22)
-i ~/.ssh/testing-server-cert is the absolute path to Notification your private key on that local computer where ~/ is starting from your home directory on Mac/*nix
joseph@TESTINGSERVER People which is the username and variable name (IP address) of the server defined at the top.

Running the Script

Now that we have the script written, you will need to save it with a .sh extension and make it read, write, and executable by the owner so if you named it logins.sh you would need to change to the directory you saved your script and run

chmod 700 logins.sh

Next, in order to actually run the script, just run

./logins.sh

within the directory that the script is located.

Alias Universal Access

Finally, if you do not want to cd (change directory) into the script directory each time, then you can put an alias in your .bash_profile (OS X) pointing it to your newly created login.sh script which allows you to access your login script from anywhere on the system. So if you saved login.sh to your home directory, you would do something like this

cd ~/
nano .bash_profile

(WITHIN NANO append this to  может  the end)
alias DOlogin="~/logins.sh"

(CTRL-X Yes Enter to save and exit)

source .bash_profile

Security vs Convenience

Now this is a convenient script, but you may cheap nfl jerseys be wondering about security. Well, you should always have a password protected private key to prevent unauthorized use and obviously keep this private key private! You should also Jersey set the appropriate permissions on your private key. Finally if someone has physical or remote access to your system, then you’re hosed anyway.

Leave a Reply

Your email address will not be published. Required fields are marked *