我编写了以下 expect 脚本以自动 ssh 登录到远程 Linux 机器 并运行命令“cat/etc/APP_VERSION.txt 文件”
有时我不会从 ssh 命令询问-
"Are you sure you want to continue connecting (yes/no)?"
所以在这种情况下,第一个 expect 仍然等待字符串“(yes/no)?” -:(
而不是继续第二个期望 - “密码:”?
如何解决? ,
我的意思是,如果“您确定要继续连接(是/否)吗?”这一行 不是来自 ssh
然后我们必须去下一个 expect "password:",但是怎么做呢?
remark1 - 如果问题“您确定要继续连接(是/否)吗?” , 然后出现 expect 脚本工作正常没有任何问题 .
remark2 - 我不能使用正超时值(如超时 10),因为 ssh 本身有延迟
. . . .
我的期望脚本:(这个期望是我的 ksh 脚本的一部分)
.
.
.
APP_MACHINE=172.12.6.190
set timeout -1
spawn ssh $APP_MACHINE
expect {
"(Are you sure you want to continue connecting yes/no)?"
{ send "yes\r" }
}
expect password: {send PASS123\r}
expect > {send "cat /etc/APP_VERSION.txt\r"}
expect > {send exit\r}
expect eof
.
.
.
. . . . .
我的 linux 机器上的 ssh 示例
ssh APP_MACHINE
The authenticity of host 'APP_MACHINE (172.12.6.190 )' can't be established.
RSA key fingerprint is 1e:8a:3d:b3:e2:8c:f6:d6:1b:16:65:64:23:95:19:7b.
Are you sure you want to continue connecting (yes/no)?
最佳答案
设置超时
set timeout 10
将等待预期的行 10 秒,如果在指定的超时时间内未收到,则继续执行脚本中的下一行。
引用资料 http://linuxshellaccount.blogspot.com/2008/01/taking-look-at-expect-on-linux-and-unix.html
更新: 如果超时不是可接受的解决方案,您可以尝试使用替代响应列表来插入脚本前进。
expect {
"(Are you sure you want to continue connecting yes/no)?" { send "yes\r"; exp_continue }
password: {send PASS123\r; exp_continue}
}
关于linux - 期望脚本+如果没有出现如何忽略字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8818383/