简体   繁体   English

如何将 bash 期望脚本链接在一起?

[英]How can I chain together bash expect scripts?

folks.伙计们。 I'm working on automating a process at work that involves entering in endless commands across a variety of VMs.我正在致力于自动化工作中的流程,该流程涉及在各种虚拟机中输入无穷无尽的命令。 It's all very simple stuff, but it's time consuming and human-error prone.这都是非常简单的东西,但它既费时又容易出现人为错误。

I stumbled across the "expect" commands and they suited my needs to begin with.我偶然发现了“期望”命令,它们一开始就适合我的需要。 I have a script that looks like this:我有一个看起来像这样的脚本:

#!/usr/bin/expect -f

spawn ssh root@[server name]
expect "such and such" 
send "thing\n"

This repeats for a bit, and eventually I finish with the work on that VM.这会重复一段时间,最终我完成了该 VM 上的工作。 What I'd like the script to do is enter an exit command, and then start doing other commands in the local terminal I'm working on, like so:我希望脚本执行的是输入退出命令,然后开始在我正在处理的本地终端中执行其他命令,如下所示:

... 
expect "last part"
send "exit\n"

expect "something"
send "new commands"

Alternatively:或者:

...
expect "last part"
send "exit\n"

spawn ./newscript.sh

For whatever reason, expect seems to ignore my last exit command.无论出于何种原因,expect 似乎都忽略了我的最后一个exit命令。 If there's a way to do multiple spawns in one expect script, or, if there's a way to chain scripts together, please let me know.如果有一种方法可以在一个期望脚本中进行多次生成,或者,如果有一种方法可以将脚本链接在一起,请告诉我。

Do this:做这个:

expect "last part"
send "exit\r"
expect eof    ;# allow the spawned process to gracefully exit

system ./newscript.sh

From the expect manual:从期望手册:

system args system参数

gives args to sh(1) as input, just as if it had been typed as a command from a terminal.args作为输入提供给 sh(1),就像它是从终端输入的命令一样。 Expect waits until the shell terminates. Expect 一直等到 shell 终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM