Tips for flexible use of expect scripts under Linux

  

For Linux system administrators who love automation, the command line tool must be used. Expect is developed by Don Libes based on the Tcl language and is widely used in interactive operations and automated testing scenarios. It is especially suitable for environments that need to perform the same operations on multiple servers, which can greatly improve system administrators. Work efficiency. This article is a technically shared article recently updated by thegeekstuff.com, which details how to execute an expect script with different command line options. What is the use, you can freely use your imagination.

This article assumes that you have some familiarity with the basic use of expect.

If you are new to the expect scripting language, you can start with our Expect's "hello world" example.

1. Use the “-c” option to execute the expect script from the command line.

expect you to use the "-c” option to execute it directly from the command line, as follows Show:

$ expect -c 'expect "\ " {send "pressed enter\ "}

pressed enter

$

If you execute the above script, it will wait for a newline character (\ ). After pressing the “enter” button, it will print out the message ““pressed enter” and exit.

2, use the “-i” option to interactively execute the expect script

Using the “-i” option, you can interactively execute the expect script with a read command from standard input. As shown below:

$ expect -i arg1 arg2 arg3

expect1.1>set argv

arg1 arg2 arg3

expect1.2>

Normally, when you execute the expect command above (there is no “-i” option), it will treat arg1 as the file name of the script, so the “-i” option allows the script to have multiple The parameters are treated as a continuous list.

This option is useful when you execute the expect script with the "-c" option. Because by default, expect is executed interactively.

3, when executing the expect script, output debugging information

When you execute the code with the “-d” option, you can output diagnostic information. As shown below:

$ cat sample.exp

# !/usr/bin/expect -fexpect "\ ";send "pressed enter";$ expect -d sample. Expexpect version 5.43.0argv[0] = expect argv[1] = -d argv[2] = sample.expset argc 0set argv0 "sample.exp"set argv ""executing commands from command file sample.exp

expect: does "" (spawn_id exp0) match glob pattern "\ "? no

expect: does "\ " (spawn_id exp0) match glob pattern "\ " ;? yes

expect: set expect_out(0,string) "\ "

expect: set expect_out(spawn_id) "exp0"

expect: set expect_out (buffer) "\ "

send: sending "pressed enter" to { exp0 pressed enter}

4, use the “-D” option to start the expect debugger
The

“-D” option is used to start the debugger, which only accepts a Boolean parameter. This parameter indicates whether the prompter must be started immediately, or just initialize the debugger and use it later.

$ expect -D 1 script

“-D” The options to the left of the option are processed before the debugger starts. Then, after the debugger is started, the remaining commands will be executed.

$ expect -c 'set timeout 10' -D 1 -c 'set a 1'

1: set a 1

dbg1.0>

5, execute the expect script line by line

Usually, expect will read the entire script into memory before executing the script. The “-b” option allows expect to read only one line of the script at a time. This is very useful when you are not writing a complete script. Expect can start executing this incomplete script, and it can avoid writing scripts to temporary files.

$ expect -b

6. Let expect not interpret command line arguments

You can use identifiers to let expect not interpret command line arguments.

You can read command line arguments like this:

$ cat print_cmdline_args.exp

#!/usr/bin/expect

puts 'argv0 : [lindex $argv 0]';

puts 'argv1 : [lindex $argv 1]';

When executing the above script, the command line options are skipped. They are treated as arguments (instead of the expect option) as follows:

$ expect print_cmdline_args.exp -d -c

argv0 : -d

argv1 : - c

Copyright © Windows knowledge All Rights Reserved