SSH Login With Password Bash Script

Shell Scripting

While checking for a script which checks for a username ssh login with password to a remote ssh host, either windows machine with openssh or a linux machine. I saw many articles on how to login to remote host password less or how to input password automatically using expect or sshpass tools. But for me the requirement was only to test the correctness of a username and password to a remote ssh login.

To complete this below script can be used which works well for Windows as well as Linux hosts for ssh login with password from a bash script.

sshtest.exp :

#!/usr/bin/env expect
set host [lrange $argv 0 0]

set username [lrange $argv 1 1]
set password [lrange $argv 2 2]

set pid [ spawn -noecho ssh $username@$host ]
set timeout 30
expect {
    "(yes/no)" {
        sleep 1
        send "yes\n"
        exp_continue
    }
    "(y/n)" {
        sleep 1
        send "y\n"
        exp_continue
    }
    password {
        sleep 1
        send "$password\n"
        exp_continue
    }
    Password {
        sleep 1
        send "$password\n"
        exp_continue
    }
    "Last login" {
       puts "Successful Login to Linux host..."
       # Un-comment the below command in case you want to continue working on the connected SSH.
       # interact
	exit 0
    }
    "Microsoft Windows" {
       puts "Successful Login to Windows host..."
       # Un-comment the below command in case you want to continue working on the connected SSH.
       # interact 
	exit 0
    }
    "Permission denied" {
        puts "Access not granted, aborting..."
        exit 1
    }
    timeout {
        puts "Timeout expired, aborting..."
        exit 1
    }
    eof {
        #puts "EOF reached."
    }
}

How To Run:

# ./sshtest.exp testserver.techpaste.com testuser welcome*12

Sample Outputs(exit code 1/0 can be used in any other scripts to check username & Password validity):

1. To a Linux Machine with SSH access:

# ./sshtest.exp ec2-11-14.compute-1.amazonaws.com root new*agetech
[email protected]'s password:
Last login: Thu Feb 4 15:58:44 2016 from 173.267.22.115
Successful Login to Linux host...

2. To a Windows Machine with OpenSSH

# ./sshtest.exp testserver.techpaste.com testuser welcome*12

****USAGE WARNING****

This is a private computer system. This computer system, including all
related equipment, networks, and network devices (specifically including
Internet access) are provided only for authorized use. This computer system
may be monitored for all lawful purposes, including to ensure that its use
is authorized, for management of the system, to facilitate protection against
unauthorized access, and to verify security procedures, survivability, and
operational security. Monitoring includes active attacks by authorized entities
to test or verify the security of this system. During monitoring, information
may be examined, recorded, copied and used for authorized purposes. All
information, including personal information, placed or sent over this system
may be monitored.

Use of this computer system, authorized or unauthorized, constitutes consent
to monitoring of this system. Unauthorized use may subject you to criminal
prosecution. Evidence of unauthorized use collected during monitoring may be
used for administrative, criminal, or other adverse action. Use of this system
constitutes consent to monitoring for these purposes.

[email protected]'s password:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Program Files\OpenSSH\home\testuser>Successful Login to Windows host...

Incase expect command is not available in your host, you can download a rpm from here and use the above script.

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.