Expect basic use method

Source: Internet
Author: User

Reference:

Http://www.cnblogs.com/lzrabbit/p/4298794.html

Expect is a command that can be used to interact with child processes in a Linux system, and it can do some automated work. Python also has a module pexpect, which provides similar functionality.

For example: Using SSH login requires a password, you can use expect instead of manual input.

For example: Use passwd to modify your account password, or you can use expect instead.

Expect executes the file interactively, executes the command with various execution methods.

1. Interactively enter expect on the command line to enter interactive

Common commands

Send the string to the child process or to the current process, and note that you want to end with \ n

Expect the expected string

Spawn opening a child process

Interact

2. Write the expect command to the file and execute the file

Expect's most commonly used grammar is the mode of the TCL language-action

Multiple branches, matched to a string, perform the subsequent action.

expect {"hi" { send "You said hi\n"}"hello" { send "Hello yourself\n"}"bye" { send "That was unexpected\n"}}

#!/usr/bin/expect-f

Set ipaddr "192.168.16.222"

Set passwd "YOURPASSWD"

Spawn ssh [email protected] $ipaddr #spawn to open a child process execution command.

# expect is executed sequentially, the braces are matched to the corresponding pattern, and the corresponding command is executed.

Expect {

"(yes/no)?" {

Send "yes\n"

Expect "Assword:" {send "$pasword \ n"}

}

"Assword:" {

Send "$password \ n"

}

}

3. Execute directly on the command line with the-c parameter

Expect-c ' expect "dddd" {send "dddd"}

Expect basic use method

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.