automate ssh login

Shell Script to Automate SSH Login

Sometimes you may need to automate SSH login on your system. There are numerous ways to do this. In this article, we will look at how to create shell script to automate SSH login – with SSH keys or with passphrase.


Shell Script to Automate SSH Login

Here are the different ways to automate SSH login.


1. With passphrase

If you log into SSH using passphrase and not SSH keys then there are many third-party tools to help you automate SSH login. For our example, we will use sshpass.

Open terminal and run the following command to install sshpass

$ sudo apt-get install sshpass

Run the following command to log into SSH using your password. Replace password, user and host with your SSH password, SSH username and SSH host IP address.

$ sudo sshpass -p your_password ssh user@hostname

You can also add the above command in a shell script. Open terminal and run the following command to create an empty shell script file.

$ sudo vi auto_ssh_login.sh

Add the following shell script with the above command.

#!/bin/sh

sudo sshpass -p your_password ssh user@hostname

Save and close the file. If the above shell script does not work for you, replace sshpass and ssh commands above with their full paths. Here is an example.

sudo usr/bin/sshpass -p your_password /usr/bin/ssh user@hostname

These paths may be different based on your system. You can use locate command to find their paths.

$ locate ssh
$ locate sshpass


2. With SSH Keys

If you don’t use passphrase, then you need to generate public RSA keys to automate SSH login. Open terminal and run the first command shown below. Just hit enter for all prompts. At the end, it will create the keys files and give you their paths.

$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.

Copy your keys in your remote SSH server using the following ssh-copy-id command. It will automatically log into your SSH server and copy keys from your local machine to remote SSH server. Replace user and host below with your remote SSH username & host IP address respectively. You will be prompted for SSH password.

$ ssh-copy-id user@host 
user@host's password: 

Log into your SSH server with the following command.

$ sudo ssh user@host

Look into the content of .ssh/authorized_keys to make sure your keys have been copied correctly.

$ ls .ssh/authorized_keys

From now on, you can easily log in with the following command.

$ sudo ssh id@server

You can also place the above command in a shell script if you want. Open terminal and run the following command to create a blank shell script.

$ sudo vi auto_ssh_login.sh

Add the following lines to it.

#!/bin/sh

sudo ssh id@server

Save and close the file.

In both the above cases, you need to make your shell script executable.

$ sudo chmod +x auto_ssh_login.sh

Run the shell script with the following command.

$ ./auto_ssh_login.sh

In this article, we have looked at two different ways to automate SSH login, and also created shell script for it.

Also read:

How to Pause Shell Script
How to Send HTML Email in Python
How to List All Installed Packages in Ubuntu
How to Find & Replace String in VI Editor
How to Find Parent Process ID in Linux

One thought on “Shell Script to Automate SSH Login

Leave a Reply

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