Linux expect usage

Source: Internet
Author: User
Tags ftp file remote ftp server

usage

1. [#!/usr/bin/expect]
This line tells the code in the operating system script to use that Shell to execute. The expect here is actually a kind of thing with bash under Linux and cmd under Windows.
Note: This line is required on the first line of the script.


2. [Set timeout 30]
Basically know English is aware that this is set timeout time, now you just remember his timing unit is: seconds. Timeout-1 is never timed out, by default, timeout is 10 seconds;


3. [Spawn ssh-l username 192.168.1.1]
Spawn is a expect internal command that can be executed after entering the expect environment, if no expect is installed or the Spawn command is not found directly under the default shell. So don't use commands like "which spawn" to find spawn commands. Like dir in Windows is an internal command that comes with the shell and you can't find an executable file for dir.com or Dir.exe.
Its main function is to add a shell to the SSH running process to pass the interactive instructions.
Spawn is followed by shell commands that need to be executed, such as Spawn sudo touch testfile


4. [Expect "Password:"]
Here expect is also a expect internal command, a bit dizzy, expect shell command and internal command is the same, but not a function, habit is good. This command means to determine whether the last output contains the string "Password:", if any, return immediately, or wait for a period of time to return, where the waiting length is the previous set of 30 seconds


5. [Send "ispass\r"]
This is where the interactive action is performed, which is equivalent to the action of entering the password manually.
Tips: The end of the command string do not forget to add "\ r", if the status of abnormal waiting can be checked.


6. [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. If you just log in past to execute


7. $ARGV parameter array
The expect script can accept parameters passed from bash. can be obtained using [lindex $argv n], n starting from 0, representing the first, second, third .... Parameters
Where $ARGC is the number of command-line arguments, $ARGV 0 is the script name itself, $argv as a command-line argument. [lrange $argv 0 0] represents the 1th parameter, [lrange $ARGV 0 4] is the first to the fifth parameter. Unlike the C language, the $ARGV does not contain the script name itself.


8. Send and Send_user
Send sends the information needed in the expect script to the spawn-initiated process, and Send_user just echoes the message from the user, similar to echo in the shell.


9. If you use the-d (debug parameter) on the first line (#!), you can output some useful information at run time.
Like you'll see
Argv[0] =/usr/bin/expect argv[1] =-D argv[2] =./launch.exp Argv[3] = 1 Argv[4] = 2 argv[5] = 3
You can also use these to complete parameter passing

Usage of exp_continue

Expect {
-re "Permission denied, please try again." {
Send_user "Error:permission denied.\n"
Exit
}
-re "is sure you want to continue connecting (yes/no)?" {
Send "yes\r"; exp_continue
}
-re "Assword:" {
Send "$loginpass \ r"; exp_continue
}
-re "Connection refused" {
Exit
}
Timeout {
Exit
}
EOF {
Exit
}
}

Once the exp_continue is used, it is re-executed from the beginning of the current expect block and can be easily understood by the continue of the while loop.


Four Commands



The most critical four commands in expect are send,expect,spawn,interact.

Send: For sending a string to a process
Expect: Receiving a string from a process
Spawn: Start a new process
Interact: Allow user interaction

1. Send command

The Send command receives a string parameter and sends the parameter to the process.

expect1.1> send "Hello world\n"
Hello World

2. Expect command
(1) Basic knowledge

The expect command is just the opposite of the Send command, and expect is typically used to wait for feedback from a process. Expect can receive a string parameter, or it can receive a regular expression parameter. In conjunction with the Send command above, we can now look at one of the simplest interactive examples:

Expect "hi\n"
Send "Hello there!\n"

These two lines of code mean that when you wait for the hi and the newline key from standard input, you output hello there to the standard output.

Tips: $expect _out (buffer) stores all input to expect,< $expect _out (0,string) > stores input that matches to the expect parameter.

For example, the following programs:

Expect "hi\n"
Send "You typed < $expect _out (buffer) >"
Send "but I only expected < $expect _out (0,string) >"

When entering in the standard input

Test
Hi

Yes, the result of the operation is as follows

You typed:test
Hi
I only Expect:hi

(2) Mode-action

The most commonly used syntax for expect is the mode-action from the TCL language. This syntax is extremely flexible, so let's explain each of the various syntaxes below.

Single branch mode syntax:

Expect "HI" {send "you said HI"}

After matching to Hi, it will output "you said HI"

Multi-branch mode syntax:

Expect "HI" {send "you said hi\n"} \
"Hello" {send "Hello yourself\n"} \
"Bye" {send "that is unexpected\n"}

Executes the corresponding output when matched to any one of the Hi,hello,bye strings. Equivalent to the following wording:

Expect {
"HI" {send "you said Hi\n"}
"Hello" {send "Hello yourself\n"}
"Bye" {send "that is unexpected\n"}
}

3. Spawn command

All of the demos above are interacting with the standard input and output, but we want him to interact with a process. The SPAWM command is used to start a new process. The send and expect commands after spawn are interactive with the spawn open process. In conjunction with the send and expect commands above, we can look at the more complex pieces of the program.

Set Timeout-1
Spawn FTP ftp.test.com//Open a new process where users connect to a remote FTP server
Expect "name"//Process returns name
Send "user\r"//input to process anonymous\r
Expect "Password:"//Process returns Password: when
Send "123456\r"//input to process [email protected]\r
Expect "ftp>"//Process returns ftp>
Send "binary\r"//input to process binary\r
Expect "ftp>"//Process returns ftp>
Send "Get test.tar.gz\r"//Enter get test.tar.gz\r to process

The purpose of this code is to log on to the FTP server FTP ftp.uu.net and download the file test.tar.gz on the server in binary mode. There are detailed comments in the program.


4.interact

So far, we have been able to combine spawn, expect, and send automation to accomplish many tasks. But how to get people to intervene in the process at the right time. For example, when downloading the ftp file, you can still stay in the FTP command line state, in order to manually execute subsequent commands. Interact can achieve these goals. The following demo allows users to interact after automatically logging on to FTP.

Spawn FTP ftp.test.com
Expect "Name"
Send "user\r"
Expect "Password:"
Send "123456\r"
Interact

Linux expect usage

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.