Recently relatively busy, in the company wrote a Centreon+nagios platform source automatic installation script (because the company can not even outside the network, the company Yum source a lot of things are not all, so can only take the source installation), which wrote to install Centreon time, execution./install-i will need to enter read license, and need to read more content, you need to press the space bar to read, the following use expect to achieve this part, if you have a better method, trouble pointing, learn from each other, because the script has been used, so inconvenient to open, here simply to cite an example, After that, we should write some open-source stuff with Python, and please give us a lot of advice.
1, installation expect
Yum-y Install expect*
2, expect and. Install-i Interactive process, the following script is named interactive.sh
#!/usr/bin/expectset timeout 45 #有些命令需要执行比较久, set 45 here to avoid expect execution of the command procedure exits the set rrdspath [lindex $argv 0] #传参, the following examples illustrate set pearpath [lindex $argv 1] spawn ./install.sh -i #需要交互的命令 expect "Press enter to continue." #有出现的字符串send "\ n" # After the above string appears, send the carriage return expect "Modification follow." #此为读取license底部出现的行send #发送多个空格send " "send " "send " "send " "expect " default to *: " #直到此处, require input select send "y\r" #自动输入中间还有很多略过 ... expect eof
2.1 Executing a expect parameter instance
The following uses SMBPASSWD batch to build the user to example
Bash script
#!/bin/bash i=1for ((i=1;i<=1000;i++)) do./sambaname user${i} [email Protected]${i} #这里就是传入参数了done
The following script is named./samaname
#!/usr/bin/expect-fset username [lindex $argv 0] #和上面类似set password [lindex $argv 1]spawn smbpasswd-a $usernameexpect "*password:" Send "$password \ n" #注意这里发送的是上面的username变量expect "*password:" Send "$password \ n" Expect EOF
The two are placed under the same directory, and then chmod a+x changes the execution permissions, and the second script goes to the first one.
This article is from the "Essays" blog, please be sure to keep this source http://yaonet.blog.51cto.com/5933544/1755079
Shell Write automatic installation script automatically read license and auto input enter