How to automate SSH login with password? ssh autologin

How to automate SSH login with password?

Before we begin, I would like to highlight that you are recommended to use public key authentication method if you want to setup login via ssh to any machines without the use of password instead of the method describe next. This is because it is more secure. There are plenty of articles in the web that explain why and please search for it if you want to know more.

Since I mentioned public key authentication is more preferred, why are we still interested in the method that we are going to explore next? Well in some machines or environments, public key authentication was not setup and you might not have the privelleges to set it up. If you happened to be in the senario decribed above and are in a hurry to ssh to a large number of machines to perform some tasks, below method might come in handy for you.

Step 1
Create a custom script using expect scripting language. As an example name the script as auto_login. The first line of the script should look like below.

Step 2
Setup three variables that will store user input for ssh login process. Basically these are hostname, userid and password.
set hostname [lindex $argv 0]
set pwd [lindex $argv 1]
set userid [lindex $argv2]

Step 3
Provide the syntax that will initiate the ssh process using the variables defined in step 2.
spawn ssh "$userid\@$hostname"

Step 4
Provide the syntax on what condition the script to start inputing the password. Usually it will be at the prompt for password. Notice the letter "P" is omitted below. This is due to some prompt wil use "P" and some "p".
expect "assword:*"

Step 5
Provide the syntax to input the password and give you the session for your task after login.
send "$pwd\r"

Basically the whole script will look like below.
set hostname [lindex $argv 0]
set pwd [lindex $argv 1]
set userid [lindex $argv2]
spawn ssh "$userid\@$hostname"
expect "assword:*"
send "$pwd\r"

Now you can try to test it out.
./auto_login "hostname" "password" "userid"
0 (0)
Article Rating (No Votes)
Rate this article
There are no attachments for this article.
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Cron YUM How to use yum-cron to automatically update RHEL/CentOS Linux
Viewed 735 times since Fri, Oct 26, 2018
How to mount software RAID1 member using mdadm
Viewed 1051 times since Wed, Oct 3, 2018
Tips to Solve Linux & Unix Systems Hard Disk Problems
Viewed 682 times since Fri, May 15, 2020
RHEL: GPT/MBR partition tables (using disks larger than 2 TiB)
Viewed 1753 times since Sun, May 27, 2018
“Too many authentication failures” with SSH
Viewed 2807 times since Mon, May 21, 2018
Applescript: Run or Call a Shell Script
Viewed 2476 times since Tue, Aug 6, 2019
RHEL7: Create and configure LUKS-encrypted partitions and logical volumes to prompt for password and mount a decrypted file system at boot.
Viewed 2709 times since Mon, Aug 6, 2018
20 Linux Command Tips and Tricks That Will Save You A Lot of Time linux
Viewed 2118 times since Thu, Apr 18, 2019
Securing /tmp and shm partitions
Viewed 685 times since Fri, May 15, 2020
How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
Viewed 368 times since Mon, Nov 23, 2020