Bash 在没有长脚本的情况下,如何将expect用于重复案例?

Bash 在没有长脚本的情况下,如何将expect用于重复案例?,bash,shell,expect,Bash,Shell,Expect,我正在使用expect来运行一个bash脚本,其中ssh的5个框,运行一个aptitude dist升级-d,将包压缩,然后将它们压缩回原始框 所有框都有相同的密码,所有框都需要输入“y”才能对dist升级说是 我当前的expect脚本很长,而且很混乱,重复了大量以下内容 ## Original box expect "password" send "Password\n" ## Box sshing to expect "password" send "Password\n" expect "

我正在使用expect来运行一个bash脚本,其中ssh的5个框,运行一个aptitude dist升级-d,将包压缩,然后将它们压缩回原始框

所有框都有相同的密码,所有框都需要输入“y”才能对dist升级说是

我当前的expect脚本很长,而且很混乱,重复了大量以下内容

## Original box
expect "password"
send "Password\n"
## Box sshing to
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
我的问题是,如何“重复”这一点,以便在要求输入密码的地方发送密码,在要求继续提示的地方发送y。这样,我就不必得到一个完美的顺序,当(有时一个关键的检查被扔进混合,我必须发送“是”)时会发生什么

全文如下:

#!/usr/bin/expect -f
set timeout -1
## This uses manualpatching.sh, used to zip up a list of the latest Debian packages
spawn ./manualpatching.sh
## BOX1
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
## BOX2
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
expect "connecting"
send "yes\n"
## BOX3
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## BOX4
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## BOX5
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## BOX6
expect "password"
send "Password\n"
expect "password"
send "Password\n"
expect "want to continue?"
send "y\n"
expect "password"
send "Password\n"
## END
expect eof

感谢Gaurav为我指明了正确的方向

我使用exp_continue精简了我的脚本,它现在更加干净和通用:

#!/usr/bin/expect -f
set timeout -1
spawn ./manualpatching.sh
expect {
      "Are you sure you want to continue connecting (yes/no)" {
            send "yes\r"
            exp_continue
      }
      "password" {
            send "Password\r"
            exp_continue
      }
      "want to continue?" {
            send "y\r"
            exp_continue
      }
}

感谢Gaurav为我指明了正确的方向

我使用exp_continue精简了我的脚本,它现在更加干净和通用:

#!/usr/bin/expect -f
set timeout -1
spawn ./manualpatching.sh
expect {
      "Are you sure you want to continue connecting (yes/no)" {
            send "yes\r"
            exp_continue
      }
      "password" {
            send "Password\r"
            exp_continue
      }
      "want to continue?" {
            send "y\r"
            exp_continue
      }
}

请出示你的剧本!好的,我现在就编辑这篇文章,让我知道你的想法,谢谢你在脚本中生成SSH会话吗?我在脚本中找不到任何
spawn
。每个框都应该有不同的IP地址,因此每次都需要生成具有不同IP地址的SSH。第四行的spawn./manualpatching.sh请显示您的脚本!好的,我现在就编辑这篇文章,让我知道你的想法,谢谢你在脚本中生成SSH会话吗?我在脚本中找不到任何
spawn
。每个框都应该有不同的IP地址,所以每次都需要生成具有不同IP地址的SSH。在第四行上生成。/manualpatching.sh我只想在“想要继续”操作块之后添加
eof
。很抱歉我的无知,那会怎么样?现在,没有任何模式会打破预期循环:所有分支都使用
exp\u continue
。期待
eof
只是一个明确的提醒,提醒您(以及维护此脚本的任何其他人)您希望生成的程序结束,然后脚本可以继续。我只想在“想要继续”操作块之后添加
eof
。抱歉,我的无知,那会怎么样?现在,没有任何模式会打破预期循环:所有分支都使用
exp\u continue
。期望
eof
只是一个明确的提醒,提醒您(以及维护此脚本的任何其他人),您期望生成的程序结束,然后脚本可以继续。