maihabunash
maihabunash

Reputation: 1702

How test SSH connectivity with expect script

I have Linux red-hat machine - version 5.X

I want to create expect script that verify if ssh login to remote machine is successfully

my test is only to get the password prompt on remote login ( I not want to enter password )

its like to test ssh connectivity

what I created until now is the following script

so if I get password prompt then script need to return 0

if not then script need to return 1

the problem with my script is that - the script return 0 even ssh login is failed ( no password prompt )

 #!/usr/bin/expect


 set LOGIN      [lindex $argv 0]
 set PASSWORD   [lindex $argv 1]
 set IP         [lindex $argv 2]




 set timeout 10
 spawn ssh -o StrictHostKeyChecking=no $LOGIN@$IP
 expect -re "(Password:|word:)"
 exit 0
 expect {
     timeout {error "incorrect password"; exit 1}
 eof
 }

please advice how to update my script ?

Upvotes: 1

Views: 2857

Answers (2)

Dinesh
Dinesh

Reputation: 16428

You have to enclose the condition in one expect as,

set timeout 10
spawn ssh -o StrictHostKeyChecking=no $LOGIN@$IP
expect {
    timeout {puts "Timeout happened";exit 0}
    eof {puts "EOF received"; exit 0}
    -nocase "password:" {puts "SSH connection is alive for the host $IP"; exit 1}
}

Have a look at the here which resembles your question.

Upvotes: 2

Darshan Patel
Darshan Patel

Reputation: 2899

sshlogin.expect :

set timeout 60

spawn ssh [lindex $argv 1]@[lindex $argv 0]

expect "yes/no" { 
   send "yes\r"
   expect "*?assword" { send "[lindex $argv 2]\r" }
   } "*?assword" { send "[lindex $argv 2]\r" }

interact

Usage :

sshlogin.expect <host> <ssh user> <ssh password>

Upvotes: 0

Related Questions