Can anyone help me in creating an expect script to just do an SSH on a list of servers and check if it was fine. I do not need to interact, do not need to fire any command on each server, I just want to do an ssh and come out, with a return code mentioning whether it was successful or not.
Expect is important here, as I do not have an option to setup a passwordless connection. Also there is a possibility that passwordless connection is setup on some of those servers.
I tried something like:
#!/usr/local/bin/expect
set timeout 10
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set prompt "(>|%|\\\\\\\$|#|]|) \$"
spawn ssh "$user\@$ip"
expect "Password:"
send "$password\r"
send "echo hello\r"
expect "hello"
send "exit\r"
But this gets stuck on the first server, and does nothing after that.
Thanks, Piyush
A generalized idea can be having a procedure to spawn the ssh and close the connection which will maintain the connections local to the scope, so that global
spawn_id
won't get affected at all.With
exp_continue
, we can handle even if any host does not have password. Basicallyexp_continue
will cause theexpect
to run again. So, among the mentioned phrase whichever comes, it will be handled. i.e. ifexpect
sees(yes/no)
, it will sendyes
, ifexpect
seespassword
, it will send the password value and so on. Then expect will continue to wait for the whole set of phrases again.The reason why I have added
yes/no
is because if suppose the host's RSA fingerprint needs to be saved.After successful login, I am echoing
Hello World
and expecting for the echoed message. If you have noticed, I have used\r\n$msg
in the expect statement. Why do we need\r\n
here ?Here is why. Whenever we send command that will be seen by the expect also and it will try to match against that too. If it matched, it will proceed as such.
Sample echo command output
The string we want to expect is already available in the
send
command. So, to make sure the matching expect string is only from the actual echoed response, I have added\r\n
which will help us in matching what is necessary.Then at last of the
proc
, I am sendingexit
command which will close the ssh connection and to match the sameeof
(End Of File) is used. In all sort of failure cases, the procedure will returnFAIL
.