在expect脚本中处理多个语句

use*_*222 3 expect

我是期待脚本的新手.

我在linux机器上写了一个ssh的expect脚本,我在不同的linux机器上遇到sshing的问题.我已经复制了脚本.

!/usr/local/bin/expect

set LinuxMachine [lindex $argv 0]

spawn ssh root@$LinuxMachine


expect "root@$LinuxMachine's password:"

send "root123\n"

expect "[root@Client_FC12_172_85 ~]#"

send "ls"

interact
Run Code Online (Sandbox Code Playgroud)

当我10.213.172.85从命令行提供第4行的期望时,它显示为" root@10.213.172.85's password:"并且登录成功

但有些linux会期待 -

The authenticity of host '10.213.172.108 (10.213.172.108)' can't be established.
RSA key fingerprint is da:d0:a0:e1:d8:7a:23:8b:c7:d8:40:8c:b2:b2:9b:95.
Are you sure you want to continue connecting (yes/no)
Run Code Online (Sandbox Code Playgroud)

在这种情况下脚本将无法正常工作.

请告诉我如何在一个expect命令中有两个expect语句.

提前致谢!!!

Jam*_*mes 9

在这种情况下你可以使用exp_continue:

expect {
      "Are you sure you want to continue connecting (yes/no)" {
            send "yes\r"
            exp_continue
      }
      "root@$LinuxMachine's password:" {
            send "root123\r"
            expect "[root@Client_FC12_172_85 ~]#"
            send "ls\r"
            interact
      }
}
Run Code Online (Sandbox Code Playgroud)

在上面,expect块等待是/否问题或密码提示.如果是后者,它会继续提供密码,期待提示,发送ls命令并给予控制权.如果是前者,它将回答"是"并重复预期阻止,准备找到密码的提示(或者甚至是/是否问题,就此而言 - 不是你需要它).

我还会包含一些带有意义消息的超时,以防某些期望选项与预期不匹配,但上述情况应该有效.

作为旁注,您不希望在脚本中设置root密码...我建议使用ssh密钥身份验证.

让我知道事情的后续.