How to install and use Linux tool Expect

How to install and use the tool Expect

Expect is a tool used to handle interaction, usually used in scenarios that require manual input of data. Expect can be used in scripts to achieve automation.

installation

First check if expect is installed in the system.

 whereis expect

The Expect tool depends on tcl, so tcl also needs to be installed.

First download and install tcl, install version 8.4.19 here.

wget https://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz
tar zxvf tcl8.4.19-src.tar.gz
cd tcl8.4.19/unix && ./configure
make
make install

Then download expect and install.

wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz
tar zxvf expect5.45.tar.gz
cd expect5.45
./configure --with-tcl=/usr/local/lib --with-tclinclude=../tcl8.4.19/generic
make
make install
ln -s /usr/local/bin/expect /usr/bin/expect

Note that the configure command here needs to use the --with-tclinclude option to pass in the path of the generic folder in the tcl installation package.

After the installation is complete, run the expect command to check whether the installation is successful.

$ expect
expect1.1> 

Basic operation

Commonly used commands in Expect script include spawn, expect, send, interact, etc.

The spawn
command is used to start a child process and execute subsequent commands

The expect
command accepts a string from the process. If the accepted string does not match the expected string, it will be blocked until it matches or waits for a timeout before continuing to execute

send
sends a string to the process, which is equivalent to manual input, usually the string needs to end with'\r'.

The interact
command transfers control to the console, and then manual operations can be performed. It is usually used to manually execute certain commands after automatic login using scripts. If there is no such statement in the script, the script will automatically exit after execution.

set timeout 30
Set the timeout time timeout to 30s, and the expect command will automatically continue execution when the blocking timeout. When timeout is configured as -1, it means that expect will block until it matches the expected string before continuing to execute. The timeout timeout is 10s by default.

[lindex $argv n]
You can use this command in a script to get the nth parameter passed in when the script is executed. Here argv is the parameter passed in, argc indicates the number of parameters passed in, and $argv0 indicates the name of the script.

In addition, we can also use the [lrange $argv sn en] command to get the sn to en parameters.

Example analysis

Here we write a script named restart_service.exp. The script first switches to the specified account, then downloads the software package to the webapps directory of tomcat, and then restarts the tomcat service.

#!/usr/bin/expect

set timeout -1

set user [lindex $argv 0]
set password [lindex $argv 1]
set cmd_prompt "# "

spawn su ${
    
    user}
expect ${
    
    cmd_prompt}
send "${password}\r"

expect ${
    
    cmd_prompt}
send "cd /opt/tomcat/webapps && wget http://host/path/to/package.war\r"
expect "100%"
# 直到wget下载任务打印出100%才表示软件包下载完成

expect ${
    
    cmd_prompt}
send "/opt/tomcat/bin/shutdown.sh\r"

expect ${
    
    cmd_prompt}
send "/opt/tomcat/bin/startup.sh\r"

expect eof
#interact
# 所有脚本必须以expect eof或者interact结束,一般自动化脚本以expect eof结束就行了

Run the expect restart_service.exp tomcat tomcat123 command, the script will use the tomcat account to download the software package and restart the tomcat service.

Guess you like

Origin blog.csdn.net/qq_42226855/article/details/112691274