Linux expect command-free interactive implementation

Source: Internet
Author: User

The expect command, a language based on TCL, has the same intrinsic principle as the early chat;

is a tool for automating the interaction between users and programs.

First, expect grammatical composition:

Expect can write a separate script:

#!/usr/bin/expect-f

Set Timeout 5

Spawn COMAND

Expect PATTERN0 {

Send "string0\r"

Exp_continue

}

Expect {

PATTERN1 {

Send "string1\r"

Exp_continue

}

PATTERN2 {

Send "string2\r"

Exp_continue

}

}

Expect "PATTERN3"

Exp_send "String3\r"

The above mixed with three different expect writing formats;

where pattern only supports GLOB wildcard rules (* and?) by default. And so on), you can support extended regular expression matching with just-re PATTERN.

Note: All {} must be preceded by a space;

Second, the Sub-command interpretation:

Set creates a variable, [lindex $argv 0] represents the first value in the reference script parameter, and [Lindex $argv 0-2] represents the No. 0 to 2nd value in the reference script parameter.

Set Timeout # Sets the time-out to wait for the end of the interactive execution and continues execution, where 1 never times out;

Send =exp_send is used for sending a string to the process, support for the code break, \ r for carriage return;
Options:
-i specifies spawn_id to send information for different spawn objects and is a key parameter for multi-program control
-s means slowly, control the transmission speed, send-s equivalent to Send_slow
Send-s {10.001} means one transfer per 10 characters with a transmission interval of 1 milliseconds
Expect accepts a string from the process, like the start of a trigger;
Expect contains the subcommands:
Exp_continue: Represents the continuation of the expect stream and resets the timeout clock by default;
-continue_timer do not reset the timeout clock, continue to time, timeout is ignored, not recommended;
Exp_send: Is the alias of send, exactly the same usage; (all Exp_command are command aliases, exactly equivalent)
Spawn start a new subprocess; each spawn has a spawn_id
Interact maintains the interaction state, the default spawn initiates the child process to execute the target command, and control is returned to the original process at the end of the execution command (that is, the user can enter!). ),

This operation is important when SSH is used to help maintain the interaction of the child process, such as when the FTP interaction completes the transfer of a file, it can be interact so that the user still stays in the FTP CLI. Interact

The EXEC command is used to support bash commands. The default expect does not support bash commands.
Close actively disconnects from the current process (primarily canceling the timing mechanism, increasing efficiency) rather than by default reading EOF from the interactive window;
Exit to exit the expect script command, you can perform some finishing work with exit:

exit-onexit{

EXEC rm/tmp/a

}

Where the-onexit option is required, after the parameters are executed, then the expect command is released.

Third, use case:

A host-free login B host (b host ip:192.168.0.2 user name: root password: 123456):

Choose to write scripts directly with expect AUTO_SSH.EXP:

#!/usr/bin/expect

Set Timeout 10

Note: Expect does not support the appearance of comment information in the same line, only line-wrapping comments

Set password [lindex $argv 0]

# The first parameter after the script is taken as the password, so use the format:./auto_ssh.exp 123456

Spawn ssh [email protected]

Expect "Password:"
Send "$password \ r"
Interact

The above simple implementation of the password-free login Host B.

Linux expect command-free interactive implementation

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.