}
if [catch {cli_close $cli1(fd) $cli1(tty_id)} } result] {
puts stderr $result
exit 1
Using the CLI Library to Run a Noninteractive Command
To run a noninteractive command, use the cli_exec command extension to issue the command, and then wait
for the complete output and the router prompt. For example, the following shows the use of configuration
CLI commands to bring up Ethernet interface 1/0:
if [catch {cli_open} result] {
error $result $errorInfo
} else {
set fd $result
}
if [catch {cli_exec $fd "config t"} result] {
error $result $errorInfo
}
if [catch {cli_exec $fd "interface Ethernet1/0"} result] {
error $result $errorInfo
}
if [catch {cli_exec $fd "no shut"} result] {
error $result $errorInfo
}
if [catch {cli_exec $fd "end"} result] {
error $result $errorInfo
}
if [catch {cli_close $fd} result] {
error $result $errorInfo
}
Using the CLI Library to Run an Interactive Command
To run interactive commands, three phases are needed:
•
Phase 1: Issue the command using the cli_write command extension.
•
Phase 2: Q&A Phase. Use the cli_read_pattern command extension to read the question (the regular
pattern that is specified to match the question text) and the cli_write command extension to write back
the answers alternately.
•
Phase 3: Noninteractive phase. All questions have been answered, and the command will run to
completion. Use the cli_read command extension to wait for the complete output of the command and
the router prompt.
For example, use CLI commands to do squeeze bootflash: and save the output of this command in the Tcl
variable cmd_output.
if [catch {cli_open} result] {
error $result $errorInfo
} else {
array set cli1 $result
}
# Phase 1: issue the command
if [catch {cli_write $cli1(fd) "squeeze bootflash:"} result] {
error $result $errorInfo
}
# Phase 2: Q&A phase
# wait for prompted question:
# All deleted files will be removed. Continue? [confirm]
if [catch {cli_read_pattern $cli1(fd) "All deleted"} result] {
error $result $errorInfo
Cisco ASR 9000 Series Aggregation Services Router System Monitoring Configuration Guide, Release 4.2.x
187
Configuring and Managing Embedded Event Manager Policies
CLI Library Command Extensions