How to login to SSH via a bash script

Endrit Qerreti

Endrit Qerreti

In this tutorial you will learn how to connect to your server through SSH without typing username, password or port every time you want to login into your server.

This method saves you time as all you have to do is run a simple bash script. Follow the steps below how to do this

Step 1 - Update your system

Update your system with the command below.

sudo apt update

Step 1 - Install sshpass

Once you have updated your system, proceed by Installing sshpass package using the command below

sudo apt install sshpass

Output

The following NEW packages will be installed:
  sshpass
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.
Need to get 11,7 kB of archives.
After this operation, 35,8 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 sshpass amd64 1.09-1 [11,7 kB]
Fetched 11,7 kB in 0s (38,6 kB/s)
Selecting previously unselected package sshpass.
(Reading database ... 642296 files and directories currently installed.)
Preparing to unpack .../sshpass_1.09-1_amd64.deb ...
Unpacking sshpass (1.09-1) ...
Setting up sshpass (1.09-1) ...
Processing triggers for man-db (2.10.2-1) ...

Step 2 - Test sshpass

Once sshpass has been installed on your system, you need to check if sshpass is working correctly. The command below will output what each command does

sshpass -h

So far there's no issue when you call help argument, but let's do a real test, let's try to login to our ssh server and see if sshpass is working fine

The correct syntax to login to a SSH server by using username and password is

sshpass -p password ssh user@ip-address
🗒️
Note: I am connecting to my localhost server that no one but my user has access, it is not recommended to use this method of logging in to a server that multiple users have access, the reason is because password can be seen on logs, I am covering this option to help you understand how sshpass works.

Read password from a file

Instead of using password as plaintext, use the -f argument which allows sshpass to read the password from a file, then set the correct permission to make this file readable only by you.

1)Create the password file

sudo nano password.txt

2)Next, type your password on the file

password

3)Save changes by pressing CTRL + X

4)Next, make password.txt readable by the owner only

sudo chmod 0400 password.txt

Now you can connect to your server by using the -f argument

sshpass -f password.txt ssh -o StrictHostKeyChecking=no -p "port" user@ip-address

Step 2 - Create bash script

Run the command below to create a bash script called ssh.sh, you can use any name you like, simply replace ssh.sh with new-name.sh

Example: We chose to call the script ssh.sh

sudo nano ssh.sh

This is the content of ssh.sh,

-p : password

ssh -o : Host key will not be checked when you try to connect to your server

-p : port

user : username

#!/bin/bash

sshpass -f password.txt ssh -o StrictHostKeyChecking=no -p "port" user@ip-address

Next, save the content of ssh.sh by pressing CTRL + X

Step 3 - Make ssh.sh Executable

To make ssh.sh executable run

sudo chmod +x ssh.sh

After making ssh.sh executable, test if it runs fine

./ssh.sh

Step 4 - Run the bash script

After creating the bash script with your server credentials, run the script

./ssh.sh

To automate this script, you can use crontab. For example: you can create a cronjob that runs ./ssh.sh and updates your server

1) Run crontab

crontab -e

2) Create task

0 0 * * 0 bash /root/ssh.sh

3) That's it!

Conclusion

In this tutorial you created a bash script that connects to your ssh server without having to type username and password or IP address, by using the sshpass utility.