Installation and use method of expect tools

Source: Internet
Author: User

Expect is a tool for dealing with interactions, usually for scenarios where you need to enter data manually, and you can use expect in your scripts to automate. installation

First check to see if there are any installed expect in the system.

# Whereis expect

Expect tools are dependent on TCL, so you also need to install TCL.

First download and install TCL, install the 8.4.19 version 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
Install

Then download the expect and install it.

# 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 the generic folder path in the TCL installation package.

After the installation is complete, run the expect command to see if the installation was successful.

# expect
expect1.1> 
Basic Operations

The commands commonly used in expect scripts include spawn, expect, send, interact, and so on. Spawn

This command is used to start a child process and execute subsequent commands expect

The command takes a string from the process and, if the accepted string does not match the expected string, blocks until the match or waits for a time-out to proceed to the send

Sends a string to the process, which is equivalent to a manual entry, usually with a string ending with ' \ R '. Interact

This command gives control to the console and can then be manually manipulated. It is typically used to automate logins after using a script and then manually execute certain commands. If this statement is not in the script, the script will exit automatically when it finishes execution. Set Timeout

Set timeout time timeout for 30s,expect command blocking timeout automatically continues to execute. Configuring the timeout to-1 indicates that the expect is blocked until it matches the expected string to continue. Timeout timeout defaults to 10s. [lindex $argv N]

You can use this command in a script to get the nth argument passed in when the script executes. Here argv is the incoming argument, the other argv is the passed-in argument, and ARGC represents the number of incoming arguments, $ARGV 0 represents the script name.

Alternatively, we can use the [lrange $argv sn en] command to obtain the SN to the en parameter. Instance Resolution

Here we write a script named Restart_service.exp, which switches to the specified account, then downloads the package to Tomcat's WebApps directory, 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%"
# until wget download task prints out 100% To indicate package download complete

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

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

expect EOF
#interact
# All scripts must end with expect EOF or interact, The general automation script ends with expect EOF.

Run the expect restart_service.exp Tomcat tomcat123 command, the script will download the package using the Tomcat account and restart the Tomcat service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.