pass ssh password shell script

How to Pass SSH Password in Shell Script

By default, when you try to login to SSH server, it will prompt you for password. But if your shell script requires automated SSH access, it can be a problem to provide SSH password every time your script runs. In such cases, it is advisable to provide SSH password within your shell script or application. In this article, we will learn how to pass SSH password in shell script.


How to Pass SSH Password in Shell Script

Here are the steps to pass SSH password and automate SSH login.


1. Install SSHPass

Typically, when you connect to your SSH server with the following command, you are prompted for password.

$ ssh username@host

SSHpass is a powerful utility that allows you to pass SSH password while connecting to SSH server. So first we will install this utility on our system. Open terminal and run the following command to do this.

#Ubuntu/Debian
$ sudo apt-get install sshpass

#Fedora/CentOS
$ yum install sshpass


2. Pass SSH Password

Next, you can specify SSH password whenever you run sshpass command as shown below. Replace YOUR_PASSWORD, YOUR_USERNAME and EXAMPLE.COM with your SSH password, username and hostname respectively.

$ sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@EXAMPLE.COM

If you want to connect to a different port than 22, you can specify it in your command as shown below. Here we connect to port 2200.

$ sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM:2200

You can also use -f option to make sshpass read password from a file.

You can call the above command from your shell script to automatically connect to your SSH server, without issuing any prompt.

In this short article, we have learnt how to provide SSH password in shell script. SSHpass is a very useful utility to automate tasks and processes that require SSH login. It also offers many options to help you customize your SSH connection.

Also read:

MySQL Change Table Engine from InnoDB to MyISAM
How to Install Fonts in Ubuntu
How to Increment & Decrement Variable in Shell Script
How to Get Current Directory of Shell Script
Apache Commands Cheat Sheet

Leave a Reply

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