在Expect脚本中处理多个语句 [英] Handle multiple statements in an Expect script

查看:210
本文介绍了在Expect脚本中处理多个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Expect脚本的新手.

我在Linux机器上为ssh编写了Expect脚本,在那里我面临着在ssh'到不同Linux机器的问题.下面,我复制了脚本.

!/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

当我在第四行的命令行中从expect提供10.213.172.85时,它表示为"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)

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

如何在一个Expect命令中包含两个Expect语句?

解决方案

在这种情况下,您可以使用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
      }
}

在上面,Expect块等待是/否问题或提示输入密码.如果是后者,它会继续提供密码,期待提示,发送ls命令并交还控制权.

如果是前者,它将回答是",并重复期望块,准备找到提示输入密码的提示(就此而言,甚至是是/否的问题-并不是您需要的). /p>

如果某些期望选项与预期不匹配,我还将在某些消息中包含一些超时,但上述方法应该可以工作.

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

I am new to Expect scripting.

I wrote an Expect script for ssh in a Linux machine, where I am facing a problem in ssh'ing to different Linux machines. Below I have copied the script.

!/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

When I supply 10.213.172.85 from command line the expect in the 4th line, it reads as "root@10.213.172.85's password:" and logs in successfully

But some Linux will expect

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)

In this case the script will not work.

How can I have two Expect statements in one Expect command?

解决方案

You can use exp_continue in such a case:

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
      }
}

In the above, the Expect block waits for either the yes/no question OR the prompt for password. If the latter, it moves on with providing password, expecting prompt, sending ls command and giving control back.

If the former, it will answer 'yes' and repeat the expect block, ready to find the prompt for a password (or even the yes/no question again, for that matter - not that you will need that).

I would also include some timeouts with meaningful messages in case some expect option does not match as expected, but the above should work.

As a side comment, you don't want to set a root password in a script... I recommend using ssh key authentication.

这篇关于在Expect脚本中处理多个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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