如何使用Expect脚本测试SSH连接 [英] How test SSH connectivity with expect script

查看:83
本文介绍了如何使用Expect脚本测试SSH连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Linux红帽机器-版本5.X

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

我想创建一个期望脚本,以验证ssh登录到远程计算机是否成功

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 )

它喜欢测试 ssh 连通性

我到目前为止创建的是以下脚本

what I created until now is the following script

因此,如果我收到密码提示,则脚本需要返回0

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

如果没有,则脚本需要返回1

if not then script need to return 1

我的脚本的问题在于-即使ssh登录失败,该脚本也返回0(无密码提示)

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 ?

推荐答案

您必须将条件包含在一个 expect 中,

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.

这篇关于如何使用Expect脚本测试SSH连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆