7 linux bash shell tk-toolkit expect
我编写以下expect脚本以自动ssh登录到远程Linux机器并运行命令"cat /etc/APP_VERSION.txt file"
有时我没有从ssh命令询问 -
"Are you sure you want to continue connecting (yes/no)?"
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下,第一个期望仍然等待字符串"(是/否)?" - :(
而不是继续第二个期望 - "密码:"?
怎么解决这个?,
我的意思是"如果您确定要继续连接(是/否)?" 是不是从ssh出现
然后我们必须转到下一个期望"密码:",但是怎么做呢?
remark1 - 如果问题"你确定要继续连接(是/否)?" ,出现然后期望脚本工作正常没有任何问题.
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
.
.
.
Run Code Online (Sandbox Code Playgroud)
.....
我的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)?
Run Code Online (Sandbox Code Playgroud)
设置超时
设置超时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}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
当你产生SSH时,请执行以下操作:
spawn ssh -o "StrictHostKeyChecking no" "$user\@ip"
Run Code Online (Sandbox Code Playgroud)