如何使用 expect 脚本测试 SSH 连接
How test SSH connectivity with expect script
我有 Linux red-hat 机器 - 版本 5.X
我想创建 expect 脚本来验证 ssh 登录到远程机器是否成功
我的测试只是在远程登录时得到密码提示(我不想输入密码)
它想测试 ssh 连通性
到目前为止我创建的是以下脚本
所以如果我收到密码提示,那么脚本需要 return 0
如果没有那么脚本需要 return 1
我的脚本的问题是 - 脚本 return 0 即使 ssh 登录失败(无密码提示)
#!/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
}
请指教如何更新我的脚本?
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
用法:
sshlogin.expect <host> <ssh user> <ssh password>
您必须将条件包含在一个 expect
中,如
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}
}
看看与您的问题相似的。
我有 Linux red-hat 机器 - 版本 5.X
我想创建 expect 脚本来验证 ssh 登录到远程机器是否成功
我的测试只是在远程登录时得到密码提示(我不想输入密码)
它想测试 ssh 连通性
到目前为止我创建的是以下脚本
所以如果我收到密码提示,那么脚本需要 return 0
如果没有那么脚本需要 return 1
我的脚本的问题是 - 脚本 return 0 即使 ssh 登录失败(无密码提示)
#!/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
}
请指教如何更新我的脚本?
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
用法:
sshlogin.expect <host> <ssh user> <ssh password>
您必须将条件包含在一个 expect
中,如
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}
}
看看与您的问题相似的