Shell Script to Automate SSH Login

Shell Script to Automate SSH Login

Almost every Linux developer uses SSH to login to their remote server. However, it can be tedious to do this manually every time. Also sometimes, you may need to login via another application or script. In these cases, you need to automate SSH login. You can easily do this by writing a shell script to automate SSH login. In this article, we will learn how to write shell script to automate SSH login. You can use this for all Linux systems.

Shell Script to Automate SSH Login

We will look at two use cases – SSH login with password as well as without password.

1. With Password

There are several third party tools to help you automate SSH login with password, instead of SSH keys. We will use sshpass for our purpose.

$ sudo apt-get install sshpass

Run the following command to log into SSH with your password. Replace your_password with your SSH password, hostname with SSH hostname and user with SSH user.

$ sshpass -p your_password ssh user@hostname

You can also create shell script to automate SSH login.

$ vi auto_ssh_login.sh

For example, add the following shell script for SSH login.

#!/bin/sh

sudo sshpass -p your_password ssh user@hostname

Save & close the file. If the above commands do not work for you, then replace sshpass with their full paths.

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

You can use locate commands to find out the location of ssh and sshpass commands.

$ locate ssh
$ locate sshpass

2. With SSH Keys

If you do not use password, you need to generate RSA keys to automate SSH login. For this purpose, open terminal and run the following command.

$ 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 SSH keys to remote server using following ssh-copy-id command. It will automatically log into your SSH server and copy SSH keys from local machine to remote machine. You will be prompted for SSH password.

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

First log into SSH server with the following command.

$ sudo ssh user@host

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

$ ls .ssh/authorized_keys

Thereafter, you can easily log into SSH via the following command.

$ sudo ssh id@server

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

$ vi auto_ssh_login.sh

Add the following lines to it.

#!/bin/sh

sudo ssh id@server

Save and close the file. Run the following command to make it an executable.

$ sudo chmod +x auto_ssh_login.sh

You can run the shell script with following command.

$ ./auto_ssh_login.sh

In this article, we have learnt a couple of simple ways to login to SSH server automatically. You can easily use these commands on terminal or include them in shell script.

Also read:

How to Force User to Change Password in Linux
How to Pause Shell Script
How to Send HTML Email in Python
How to Find & Replace String in VI Editor
How to List Installed Packages in Linux

Leave a Reply

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