Expect Study Notes (1) Author: Badcoffee
Email: blog.oliver@gmail.com
November 2004
Original article: http://blog.9cbs.net/yayong
Copyright: Please be sure to indicate the original source, author information and this statement by hyperlink.
Contacting Expect is forced to have. System administrators often encounter such problems in their work, need to implement an automatic interaction tool, this
The tool can automatically Telnet or FTP to the specified server, and some commands are automatically executed after successful login to complete the required work.
Of course, there are many programming languages to solve such problems, such as C, Perl, or Expect.
Obviously, although c is omnipotent, solving such problems is still more difficult, unless you are familiar with Telnet or FTP protocol.
I have seen someone to use C to implement a simple Telnet client protocol program, which can join your code in this program to capture the server.
The output is transmitted for remote control based on these outputs.
Such functions can be implemented like using Perl, however, Expect is better, and in addition to support
Outside the UNIX / Linux platform, it also supports
Windows
Platform, it is generated for automatic interaction between system management and software testing:
Expect is a free programming tool language that implements automatic and interactive tasks to communicate without people's intervention.
Expect author Don Libes has the following definitions when writing Expect in 1990:
EXPECT is a software suite for automatic interactive features (Expect "intective tools.
The Expect language is based on TCL
As a scripting language, TCL has a simple syntax:
CMD Arg Arg Arg
A TCL command consists of a word split. The first word is a command name, and the rest is the command parameter.
$ foo
The $ symbol represents the value of the variable. In this example, the variable name is foo.
[cmd arg]
Square brackets perform a nested command. For example, if you want to pass the result of a command as a parameter of another command, then you use this symbol.
"Some Stuff"
Dual quotation marks a parameter of the word group as a command. "$" Symbol and square brackets are still explained in double quotes.
{Some stuff}
The braces also labeled the phrase as a parameter of the command. However, other symbols are not interpreted in braces.
/
The reverse slope symbol (/) is used to reference special symbols. For example: / n represents the wrap. The anti-sliding line symbol is also used to close the special meaning of "$" symbol, quotation marks, square brackets and braces.
The best way to learn is to learn from the stem of stems. For those who have been familiar with a programming language, write the program with another new language to solve the problem, it is very
Easy things. Therefore, after about understanding the basic syntax, I will solve the problem while checking the manual.
For grammar for TCL and Expect, please refer to
The automation related part of the UNIX / Linux platform task.
Example 1: The following is an expect script that automatically executes commands on a Telnet to the specified remote machine. The output of this script is as follows:
# / Usr / bin / expect sample_login.exp root 111111spawn telnet 10.13.32.30 7001 Trying 10.13.32.30 ... Connected to 10.13.32.30.Escape character is '^]' accho console login:. RootPassword: Last login: Sat Nov 13 17:01:37 on consoleSun Microsystems Inc. SunOS 5.9 May 2004 # Login Successfully ... # uname -psparc # ifconfig -alo0: flags = 2001000849
# vi Sample_login.exp:
PROC DO_CONSOLE_LOGIN {login pass} {set timeout 5 set done 1 set timeout_case 0 while ($ done) {expert {"console login:" {send "$ login / n"} "password:" {send "$ pass / n" } "#" {Set done 0 seund_user "/ n / nlogin successfully ... / n / n"} Timeout {switch - $ timeout_case {0 {send "/ n"} 1 {send_user "send a return ... / N "send" / n "} 2 {PUTS stderr" login time out ... / n "exit 1 }}}}}}}}}}} Proc DO_EXEC_CMD {} {
Set Timeout 5
Send "/ n"
EXPECT "#"
Send "uname -p / n"
EXPECT "#"
Send "ifconfig -a / n"
EXPECT "#"
Send "exit / n"
Expect "Login:"
Send_user "/nfinished.../n/n"
}
IF {$ argc <2} {
Puts stderr "USAGE: $ argv0 login passwaord./n"
EXIT 1
}
Set login [lindex $ argv 0]
SET Pass [Lindex $ Argv 1]
Spawn Telnet 10.13.32.30 7001
Do_console_login $ login $ pass
DO_EXEC_CMD
Close
EXIT 0
The above script is just an example. In actual work, you only need to re-implement the do_exec_cmd function, you can solve similar problems.
In Example 1, you can also learn the grammar of the following TCL:
Command line parameters
$ argc,
$ Argv 0,
$ ARGV 1 ...
$ argv n
IF {$ argc <2} {
Puts stderr "USAGE: $ argv0 login passwaord./n"
EXIT 1
}
2. Input and output
Puts stderr "USAGE: $ argv0 login passwaord./n"
3. Nested command
Set login [lindex $ argv 0]
SET Pass [Lindex $ Argv 1]
4. Command call
Spawn Telnet 10.13.32.30 7001
5. Function definition and call
Proc DO_CONSOLE_LOGIN {login pass} {............}
6. Variable assignment
SET DONE 1
7. Circulation
While ($ done) {..............}
8. Conditional branch
Switch
Switch - $ TIMEOUT_CASE {0 {.............} 1 {.............} 2 {... .......}}
9. Operation
InCr Timeout_case In addition, you can also see the following commands of Expect:
Send
EXPECT
Send_user
You can debug the expect script with the -d parameter:
# / usr / bin / expect -d sample_login.exp root 111111 ...... debug output and program output ....... Reference document:
Automation for UNIX / Linux platform tasks
EXPECT Tutorial Chinese Edition