Expect everywhere.
The first time I saw expect this command was the first time I was in full volume that's one of the company's cows. A set of automatic deployment, MD5, release of the full online tool written by the shell script, when it's okay, look at the next few scripts, a lot of expect commands. It is really difficult to understand the use of this expect command, so find time to sum up this article on the expect command.
Throw a question first
Now there are two Linux hosts A and B, how to SSH from a host to B host, and then execute the command on the B host, how to make this process fully automated? You may use this method:
SSH admin@10.220.20.15 "ls"
But this is a clumsy way to enter a password every time and not execute complex logic or commands. So how to achieve full automation? This will be used to today's summary of the expect of this article.
What is expect?
Expect is a free programming tool to automate interactive tasks without human intervention. Frankly speaking, expect is a set of software used to realize automatic interactive function.
In practice, when we run commands, scripts, or programs, these commands, scripts, or programs need to enter certain instructions that continue to run from the terminal, and these inputs need to be done manually. The use of expect, you can according to the program prompts, analog standard input provided to the program, so as to achieve automated interactive execution. This is expect!!!.
Expect Foundation
When using expect, you are basically dealing with the following four commands:
Command function
Send sends a string to the process
Expect receive string from process
Spawn start a new process
Interact allows users to interact
The Send command receives a string parameter and sends the parameter to the process.
Expect command and send command in contrast, expect is usually used to wait for the feedback of a process, and we send the corresponding interaction commands according to the feedback of the process.
The Spawn command is used to start a new process, and the spawn send and expect commands interact with processes that are opened with spawn.
Interact command is not in fact a lot, in general, using spawn, send and expect commands can be very good to complete our task, but in some special occasions, the need to use the Interact command, the Interact command is mainly used to exit automation, into human interaction. For example, we use the spawn, send and expect commands to complete the FTP landing host, perform the download file task, but we hope that after the end of the file download, still can remain in the FTP command line state, so that the manual execution of subsequent commands, Using the Interact command at this point is a good time to do this task.
Practical Code Analysis
The above is a summary of expect, especially some commonly used commands are described in detail. Here are some commonly used expect to explain how to use expect to accomplish some of the day-to-day work.
The code is as follows |
Copy Code |
#!/usr/tcl/bin/expect Set Timeout 30 Set Host "101.200.241.109" Set username "root" Set Password "123456" Spawn ssh $username @ $host Expect "*password*" {send "$password \ r"} Interact |
This is a very simple expect sample code that demonstrates the basics of how to use expect.
#!/usr/tcl/bin/expect: Use expect to interpret the script;
Set Timeout 30: Set timeout, in seconds, by default is 10 seconds;
Set host "101.200.241.109": setting variable;
Spawn ssh $username @ $host: Spawn is a expect internal command that can be executed after entering the expect environment, and cannot be found without expect or directly executing under the default shell. Its main function is to the SSH operation Process plus a shell, used to transfer interactive instructions;
Expect "*password*": Here The expect is also an internal command of expect, which means to determine whether the last output contained a "password" string, if any, to return immediately, or to wait for some time to return, The waiting time here is 30 seconds before setting;
Send "$password \ r": When matching to the corresponding output result, sends the password to the open SSH process, performs the interactive action;
Interact: After the completion of the implementation of the interactive State, the control authority to the console, this time can be manually operated. If you do not have this login completed, you will quit, rather than stay on the remote terminal.
This is an analysis of the simple and simple script mentioned above, which involves a very important concept in expect-pattern-action, the meaning of the code expressed in the expect "*password*" {send "$password \ r"}.
Mode-Action
Combined with expect "*password*" {send "$password \ r"} This code says "mode-action". To put it simply is to match a pattern, to perform the corresponding action, and to match the password string, enter the password. You may also see code like this:
The code is as follows |
Copy Code |
Expect { "Password" { Send "$password \ r" Exp_continue } Eof { Send "EOF" } } |
Where exp_continue represents a cyclic matching, usually after the match will exit the statement, but if there is exp_continue can be continuously loop matching, input more than one command, simplified writing.
Pass Reference
Many times, we need to pass parameters to the script, and now look at the following code to see how to use parameters in expect:
The code is as follows |
Copy Code |
#!/usr/tcl/bin/expect If {$ARGC < 3} { Puts "Usage:cmd Exit 1 } Set Timeout-1 Set host [lindex $ARGV 0] Set username [lindex $argv 1] Set password [lindex $argv 2] Spawn ssh $username @ $host Expect "*password*" {send "$password \ r"} Interact |
In expect, $ARGC represents the number of parameters, and the parameter values are stored in $argv, such as taking the first argument [lindex $argv 0], and so on.
Summarize
The ability to use the shell script skillfully at work can greatly improve work efficiency, if you go with expect, then a lot of work can be automated, the expansion of the job. If you can python, your vision will be more open, then you will "dislike" expect.