Using expect for Shell auto-interaction

Source: Internet
Author: User

Where the shell script needs to interact with the here document is the implementation, but some commands require the user to manually go on the interaction such as passwd, SCP

It is very painful to eliminate user interaction for automatic deployment, expect can solve this kind of problem very well.

The core of expect is spawn expect send set

Spawn calling the command to execute


Expect waits for the command prompt to appear, which is the prompt to capture user input:


Send sends values that need to be interacted with instead of manually entering content by the user


Set Variable Value


Interact after the completion of the implementation of the interactive State, the control to the console, this time can be manually operated. If this is not done, it will exit instead of remaining on the remote terminal.
Expect EOF this must be added, corresponding to the spawn to indicate that the capture terminal output information is terminated, similar to IF....ENDIF

Expect scripts must end with interact or expect EOF, and automating tasks usually expect EOF is enough.

Set expect never timeout
Set Timeout-1

Set expect 300-second timeout, if more than 300 no expect content appears, then launch
Set Timeout 300

Expect writes the syntax, expect uses the TCL syntax.

A TCL command consists of words separated by spaces. Where the first word is the command name and the rest is the command parameter
CMD arg arg arg

The $ symbol represents the value of the variable. In this case, the variable name is foo.
$foo

The square brackets execute a nested command. For example, if you want to pass the result of a command as a parameter to another command, then you use this symbol
[cmd Arg]

Double quotes mark the phrase as a parameter of the command. The "$" symbol and square brackets are still interpreted inside the double quotation marks
"Some stuff"

Curly braces also mark the phrase as a parameter of the command. However, other symbols are not interpreted in curly braces
{Some stuff}

Backslash symbols are used to refer to special symbols. For example, n means wrapping. Backslash symbols are also used to close the "$" symbol, the special meaning of quotation marks, square brackets and curly braces

expect use instances

1. First confirm that the expect package is to be placed.

#rpm-qa | grep expect

If not, you need to download the installation,

#yum Install expect

2. After the installation is complete, check the path of the expect, which can be

#which expect

/usr/bin/expect

3. Editing scripts
#vi autosu.sh
Add the following content

1 2 3 4 5 6 #!/usr/bin/expect-f//This expect path is the result of which expect view spawn Su-nginx//Switch User expect "password:" Prompt to enter password send "TESTR"//input Nginx password interact//operation completed

4. Determine if the script has executable permissions

chmod +x autosu.sh

5. Execute script expect autosu.sh or./autosu.sh

Expect common scripts

Log on to the remote server

1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/expect set timeout 5 set server [Lindex $ argv 0] Set user [lindex $ argv 1] Set PA SSWD [Lindex $ argv 2] Spawn ssh-l $ user $ server expect {& quot;   (yes/no) & quot;   {Send & quot; yesr & quot;; Exp _continue} & quot;   Password: & quot;   {Send & quot; $ passwdr & quot; }} expect & quot;   * Last login* & quot; Interact

SCP Copy File

1 2 3 4 5 6 7 8 9 Ten-All-in-one #!/usr/bin/expect s et  Timeout   set  host   [lindex   $ argv   0]       &nb sp; //1th parameter, other 2,3,4 parameters similar to set  username   [lindex   $ argv   1] set  password   [lind Ex   $ argv   2]
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.