Simple usage of shell expect

Source: Internet
Author: User

Why do I need expect?
Through the shell we can achieve simple control flow functions, such as: cycle, judgment and so on. But for situations that require interaction, it is necessary to intervene manually, and sometimes we may need to implement interactive programs such as Telnet Server, or require user interaction to enter a password when logging in to the root user. And expect is used to implement automatic interaction tools.

Expect is a free programming language that enables automated and interactive tasks to communicate without the need for human intervention. Expect's author, Don Libes, began writing Expect in 1990 with the following definition of Expect: Expect is a software suite for automatic interactivity (Expect [is a] software suite for automating Interactive tools). Using it, the system administrator can create scripts to provide input to commands or programs that are expected to be entered from the terminal (terminal), which in general need to be entered manually. Expect can simulate the input provided by the standard input to the program according to the program's prompt to realize the interactive program execution. Can even implement simple BBS chat robot. :)


Installation of Expect:

sudo apt-get install expect

Simple usage of Expect:
The following is a expect script that can be used to implement the command automatically:

#!/usr/bin/expect # Change a login shell to tcsh set user [lindex $argv 0] Spawn Chsh $user expect "]:" Send "/BIN/TCSH" expect EOF exit

This simple script can explain the characteristics of many expect programs. As with other scripts, the first line specifies the command program used to execute the script, which is/usr/bin/expect. The first line of the program is used to get the execution parameters of the script (which is stored in the array $argv, starting from number No. 0 is the parameter) and saving it to the variable user.

The second parameter uses expect's Spawn command to start the session of the script and command, which starts with the CHSH command, which actually runs in the way of the derived subprocess.

Subsequent expect and send commands are used to implement the interactive process. The script waits for the output to appear in the first place]: string, once in the output CHSH output to the feature string (the general feature string is often waiting for the input of the last hint of the feature information). For other mismatched information, it is completely ignored. When the script gets the feature string, expect sends/bin/tcsh and a carriage return to the CHSH command. The final script waits for the command to exit (Chsh end), and once it receives the EOF character that identifies the child process has ended, the expect script exits the end.


If you want to implement a remote CP, then you can write this

test.sh

#/bin/bashset Timeout 160spawn SCP [email protected]:/pathnaem/filename. Expect "Password:" send "wangyp\r" expect EOF

Then you can't use SH test.sh, but use expect test.sh

This article is from the "canvas shoes can walk the cat step" blog, please be sure to keep this source http://9409270.blog.51cto.com/9399270/1970101

Simple usage of shell expect

Related Article

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.