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.
#!/usr/bin/expect

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"
interact


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

Now you can try to test it out.
./auto_login "hostname" "password" "userid"
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
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
Linux: Disks diagnostic using smartctl
Viewed 15332 times since Wed, Jul 25, 2018
Odpalenie polecenia tylko na jedną godzinę
Viewed 2999 times since Thu, May 24, 2018
Tip: SSD and Linux. Enable TRIM and check if it works
Viewed 17300 times since Fri, May 15, 2020
Check a Website Availability from the Linux Command Line
Viewed 6849 times since Mon, Feb 18, 2019
high swap space utilization in LINUX
Viewed 6719 times since Fri, Jul 13, 2018
YUM How to use yum command on CentOS/RHEL
Viewed 7419 times since Thu, Oct 25, 2018
Linux ssh Hide OpenSSH Version Banner
Viewed 52354 times since Wed, Apr 22, 2020
LVM: Extend SWAP size by growing existing Logical Volume
Viewed 2648 times since Sat, Jun 2, 2018
debian Install a newer kernel in Debian 9 (stretch) stable
Viewed 1956 times since Sun, Sep 23, 2018
Linux File Systems (mkfs, mount, fstab) ext4
Viewed 3343 times since Sat, Jun 2, 2018