expect is a process for interacting 's command. With expect, we can write the interactive process on a script so that it is done automatically. The image of the said, SSH login, FTP login and so on are consistent with the definition of interaction. Here we first ask a question, then introduce the basic knowledge of the four commands, and finally propose a workaround.
#!/bin/bashexpect-c "Spawn ssh [email protected] \" Ls;\ "expect { \" *assword\ "{set timeout; send \" Password\r\ ";} \ "yes/no\" {send \ "yes\r\"; exp_continue;}} Expect EOF "
How do I ssh from machine A to machine B, and then execute the command on machine b? How do I automate it?
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: Starting 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: After the standard input medium to hi and the newline key, 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
Testhi
Yes, the result of the operation is as follows
You typed:testhii 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" { " you Said hi \n " \ "hello" { send "Hello yourself \n \ "Bye" { send \n "
when matching to hi,hello,bye any one string, Executes the corresponding output. is equivalent to the following wording:
expect { { send \n " "hello" { send "Hello yourself \n " "bye" { send \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 the user connects to the remote FTP server expect "Name" //Process returns name Send the user\ r" //input anonymous\r to process expect "Password:" //Process return password: when Send "123456\ r" //input to process [email protected]\r expect "Ftp>" //When the process returns ftp> Send "Binary\ r" //input binary\r to process expect "Ftp>" //When the process returns ftp> Send "Get test.tar.gz\ r" //Input 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 the user \ r " expect "Password:" Send "123456 \ r " Interact
Workaround
The above mentioned:
How do I ssh from machine A to machine B, and then execute the command on machine b? How do I automate it?
The following script implements a login from machine A to machine B, then executes the PWD command on machine B and stays on the B machine, waiting for user interaction. Please refer to the above for specific meanings.
#!/home/tools/bin/64/expect-f Set Timeout -1 Spawn SSH $buser@$Bhost expect "*password:" { Send "$password\ r" } expect "$*" { Send the PWD\ r" } Interact
[Linux Comm]expect