Drive a restricted or menu CLI with Expect
Some devices don’t give you a normal shell. They show a menu, ask you to confirm a notice, or page output with no way to turn the pager off. Expect handles these by scripting the conversation: wait for this text, send that reply. This recipe adds the part most Expect scripts leave out, which is making sure only the config reaches stdout.
When to use it
- The device’s CLI is restricted, so a connection template can’t read to a stable prompt. The Zyxel GS1900 range is one example.
- The login involves a menu or a notice to confirm, as on a Radware Alteon (“Confirm seeing above note”).
- The output is paged and the pager can’t be switched off.
For an SSH device that Netmiko already supports, use Netmiko instead. It saves you writing the conversation by hand.
The problem this recipe solves
By default Expect copies everything the device sends to stdout: the login banner, the password prompt, the command you typed, every --More-- and the final prompt. rConfig stores all of it as the config. Worse, some of it changes between runs (a “last login” line, for example), so every backup shows a change.
The fix is to turn that copying off with log_user 0 and collect the output yourself. The script below gathers the command’s output into a variable, removes the pager text, the echoed command and the trailing prompt, and prints what is left.
The template
main: name: "SIE - Expect restricted CLI" desc: "Drives a restricted CLI with Expect and stores the cleaned output"
connect: timeout: 120 idletimeout: 60 idleTimeout: 60 protocol: script
# exit_codes is deliberately left out: any non-zero exit fails the run.failure_criteria: error_patterns: - "ERROR:" success_patterns: - "SIE-OK: expect"The command
/usr/bin/expect {script_path}expect-restricted-cli.exp {device_ip} {device_username} {device_password}Check the Expect path with which expect on your server.
The script
Save it as expect-restricted-cli.exp in the scripts folder. The settings at the top are the parts to change for your device.
#!/usr/bin/expect -f## Collect a config from a restricted or menu-driven CLI for the rConfig# Script Integration Engine.## Usage: expect-restricted-cli.exp <ip> <username> <password>## Only the config goes to stdout. Progress, errors and the success marker go# to stderr. Exit code 0 on success, 1 on any failure.
# ---- Settings to adapt for your device ------------------------------------set config_command "show running-config"set prompt_re {[>#] ?$}set more_re {--More--|-- MORE --|Press any key to continue}set known_hosts "/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys/known_hosts"# ---------------------------------------------------------------------------
proc fail {message} { puts stderr "ERROR: $message" exit 1}
if {[llength $argv] < 3} { fail "usage: expect-restricted-cli.exp <ip> <username> <password>"}set ip [lindex $argv 0]set user [lindex $argv 1]set pwd [lindex $argv 2]
# Nothing the device sends is copied to stdout unless we print it ourselves.log_user 0# Expect only keeps 2000 bytes in its buffer by default. A config page can be bigger.match_max 1000000set timeout 20
puts stderr "connecting to $ip as $user"spawn ssh -o StrictHostKeyChecking=accept-new \ -o UserKnownHostsFile=$known_hosts \ -o ConnectTimeout=10 \ $user@$ip
# ---- Log in: answer whatever the device asks until we reach a prompt -------expect { -re {(?i)are you sure you want to continue connecting} { send -- "yes\r"; exp_continue } -re {(?i)password:} { send -- "$pwd\r"; exp_continue } -re {(?i)confirm seeing above note} { send -- "y\r"; exp_continue } -re {(?i)press any key to continue} { send -- " "; exp_continue } -re {(?i)(access denied|permission denied|login incorrect)} { fail "authentication failed for $user@$ip" } -re $prompt_re {} timeout { fail "timed out logging in to $ip" } eof { fail "connection to $ip closed during login" }}
# ---- Run the command and collect every page --------------------------------set timeout 60set raw ""send -- "$config_command\r"expect { -re $more_re { append raw $expect_out(buffer) send -- " " exp_continue } -re $prompt_re { append raw $expect_out(buffer) } timeout { fail "timed out waiting for '$config_command' to finish" } eof { fail "connection closed while reading '$config_command'" }}
send -- "exit\r"expect eof
# ---- Clean what we collected ----------------------------------------------regsub -all $more_re $raw "" raw ;# pager textregsub -all {\x1b\[[0-9;]*[A-Za-z]} $raw "" raw ;# terminal escape codesregsub -all {[\x08\r]} $raw "" raw ;# backspaces and carriage returns
set lines [split $raw "\n"]set lines [lrange $lines 1 end-1] ;# drop the echoed command and the final promptset config [string trim [join $lines "\n"]]
if {$config eq ""} { fail "'$config_command' returned no output, refusing to store an empty config"}
puts $configputs stderr "SIE-OK: expect [llength $lines] lines from $ip"exit 0What gets stored
Only the output of the command, with no banner, prompts or pager text:
hostname sw-edge-01interface port-channel 1description uplink-to-coreip address 192.0.2.30 255.255.255.0ip default-gateway 192.0.2.1Adapting it
- Different prompts or pager text. Change
prompt_reandmore_re. Runsshto the device by hand and copy the exact text it shows. - A menu before the CLI. Add a line to the login block for each menu screen: the text to wait for, and the key to send.
- A pager you can turn off. Send the command that turns it off (
lines 0on an Alteon,terminal length 0on many others) beforeconfig_command, and wait for the prompt. That is more reliable than answering the pager. - The Alteon walkthrough. The main docs have a full Radware Alteon example that covers the rConfig side step by step.