CliDriver - execute and control subprocesses with Expect
package require CliDriver
set p [CliDriver::Spawn #auto telnet localhost]
set count 0
set timeout 5
$p expect {
"*assword*:*" {
$p send "password\n"
exp_continue
}
"*ogin*:*" {
$p send "user\n"
exp_continue
}
"*user@host*" {
# done
}
timeout {
incr count
if {$count > 3} {
error "Timed out during login procedure."
}
exp_continue
}
}
The CliDriver API is an object-oriented wrapper around expect(1). It allows
you to automate shell sessions by launching and controlling interactive command
line applications from a Tcl script.
The main goal of the module is to alleviate the need for juggling with spawn ids and to encapsulate and simplify initialization sequences and subprocess management.
method expect ??-opts? pat1 body1? ... ?-opts? patn bodyn
Works exactly like the traditional `expect` call described in `expect`(1) but is always bound to the subprocess associated with this object.
method send ?-flags? string
Works exactly like the traditional `send` call described in `expect`(1) but is always bound to the subprocess associated with this object.
method closeCloses the connection to and terminates the subprocess associated with this object.
method killKills the subprocess. On Unix this is equivalent to sending a SIGKILL.
method match_max ?size?Sets or gets the maximum match buffer size for the `spawn_id` associated with this object.
method pidReturns the process ID of the subprocess.
method process_existsReturns true while the subprocess has not exited.
method terminateTerminates the subprocess. On Unix this is equivalent to sending a SIGTERM.
Inherits all methods from CliDriver::Core.
constructor cmd ?arg arg ...?Launch *cmd* and pass on any additional arguments unmodified.
Inherits all methods from CliDriver::Core.
constructor ?arg arg ...? host
Launch the `telnet` command installed on the system and pass on any additional arguments unmodified.
Inherits all methods from CliDriver::Core.
constructor ?arg arg ...? user@host
Launch the `ssh` command installed on the system and pass on any additional arguments unmodified.
Inherits all methods from CliDriver::Core.
constructor ?-baud int? ?-parity bool? ?-data_bits int? ?-stop_bits int? port
Create an expect session on the given serial *port* to talk to a program at the other end of a serial line, where *port* denotes a serial device node, such as `/dev/ttyS0`.
Deleting a driver object that is associated with an active process, using
itcl::delete object, will implicitely terminate the process.
expect(1), itcl(3)