I am to automate the installation phase of a legacy system, because I do not want to put more efforts again and again when ever I want to install it. During the installation process on Linux Terminal, I have to answer some questions regarding the basic configurations. Indeed, it is easy to automate the shell commands by putting them all in a batch file like the following:
我要自动化遗留系统的安装阶段,因为我不希望在我想要安装它时一次又一次地付出更多努力。在Linux终端上的安装过程中,我必须回答一些有关基本配置的问题。实际上,通过将它们全部放在如下所示的批处理文件中,可以很容易地自动执行shell命令:
bin/startServer destination/sub_v1
bin/startAdminInterface
....
However, I do not know how to automate the answers of a specific questions like the following:
但是,我不知道如何自动化特定问题的答案,如下所示:
Enter the server's IP address:
Enter the email of the contact person:
Would you like to disable UDP services?(y/n) [n]:
....
Is there any tool or programming language can deal with this situation? or Is there any way to pass the answers as a parameters within the batch file?
是否有任何工具或编程语言可以处理这种情况?或者有没有办法将答案作为批处理文件中的参数传递?
Thanks in advance.
提前致谢。
2 个解决方案
#1
3
The classic Linux tool for this job is expect.
这项工作的经典Linux工具是期待的。
With expect
one can expect different questions and variations on a question, and the question does not have to be typed exactly. expect
does not blindly answer every prompt, but rather it provides answers to the questions actually asked.
期望在一个问题上可以预期不同的问题和变化,并且不必完全键入问题。期望不会盲目地回答每个提示,而是提供实际问题的答案。
Here is a short example:
这是一个简短的例子:
#!/usr/bin/expect -f
spawn someScript.sh
expect "Enter the server's IP address:"
send "10.0.0.4\r"
expect "Enter the email of the contact person:"
send "foo@bar.com\r"
expect "Would you like to disable UDP services?(y/n) [n]:"
send "y\r"
#2
1
So, imagine this is a simplified version of the script:
所以,想象一下这是脚本的简化版本:
#!/bin/bash
read -p "Enter something:" thing
read -p "Enter server IP:" ip
read -p "Enter Email:" email
echo Received $thing, $ip, $email
and this is in a file called answers
这是一个名为答案的文件
thingywhatnot
11.12.33.240
bozo@brains.com
You would run
你会跑
installationScript < answers
and it would print this
它会打印出来
Received thingywhatnot, 11.12.33.240, bozo@brains.com
#1
3
The classic Linux tool for this job is expect.
这项工作的经典Linux工具是期待的。
With expect
one can expect different questions and variations on a question, and the question does not have to be typed exactly. expect
does not blindly answer every prompt, but rather it provides answers to the questions actually asked.
期望在一个问题上可以预期不同的问题和变化,并且不必完全键入问题。期望不会盲目地回答每个提示,而是提供实际问题的答案。
Here is a short example:
这是一个简短的例子:
#!/usr/bin/expect -f
spawn someScript.sh
expect "Enter the server's IP address:"
send "10.0.0.4\r"
expect "Enter the email of the contact person:"
send "foo@bar.com\r"
expect "Would you like to disable UDP services?(y/n) [n]:"
send "y\r"
#2
1
So, imagine this is a simplified version of the script:
所以,想象一下这是脚本的简化版本:
#!/bin/bash
read -p "Enter something:" thing
read -p "Enter server IP:" ip
read -p "Enter Email:" email
echo Received $thing, $ip, $email
and this is in a file called answers
这是一个名为答案的文件
thingywhatnot
11.12.33.240
bozo@brains.com
You would run
你会跑
installationScript < answers
and it would print this
它会打印出来
Received thingywhatnot, 11.12.33.240, bozo@brains.com