Back up a device through its console port
Some equipment has no network management port at all, only a serial console. OT controllers, older switches and out-of-band networks are the usual cases. A console server (Opengear, Lantronix, Perle and others) puts those serial ports on the network. This recipe connects to the right port on the console server, wakes the console, logs in, collects the config, and logs out so the port isn’t left open for the next person.
When to use it
- The device is only reachable through its serial console.
- You already use a console server for out-of-band access, and want backups to follow the same path.
How the pieces fit
| rConfig device field | What to enter |
|---|---|
| IP address | The console server’s address |
| Username and password | The device’s login, used at the console’s login prompt |
The console server itself is reached with an SSH key from the scripts keys folder, so its login never needs to be on the command line. The serial port number is fixed per device, in the command.
Console servers reach a port in one of two common ways. Check yours:
| Style | Example |
|---|---|
| A TCP port per serial port | ssh -p 3001 backup@console.example.com (Opengear uses 3000 plus the port number) |
| The port in the username | ssh backup:port01@console.example.com |
The script uses the first style. Change the spawn line for the second.
The template
main: name: "SIE - Console port backup" desc: "Collects a config through a console server"
connect: timeout: 300 # serial consoles are slow, often 9600 baud idletimeout: 120 idleTimeout: 120 protocol: script
# exit_codes is deliberately left out: any non-zero exit fails the run.failure_criteria: error_patterns: - "ERROR:" success_patterns: - "SIE-OK: console"The command
The second argument is the console server’s TCP port for this device’s serial port.
/usr/bin/expect {script_path}console-backup.exp {device_ip} 3001 {device_username} {device_password}The script
Save it as console-backup.exp in the scripts folder.
#!/usr/bin/expect -f## Collect a config through a console server for the rConfig Script Integration# Engine.## Usage: console-backup.exp <console_server> <tcp_port> <device_user> <device_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 ------------------------------------------------------set keys "/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys"set console_user "backup"set config_command "show running-config"set paging_off "terminal length 0"set prompt_re {[\w.-]+[>#] ?$}set busy_re {(?i)(port (is )?(in use|busy)|already connected)}# ---------------------------------------------------------------------------
proc fail {message} { puts stderr "ERROR: $message" exit 1}
if {[llength $argv] < 4} { fail "usage: console-backup.exp <console_server> <tcp_port> <device_user> <device_password>"}lassign $argv console port dev_user dev_pwd
log_user 0match_max 1000000set timeout 20
puts stderr "connecting to $console port $port"spawn ssh -p $port -i $keys/console_ed25519 \ -o BatchMode=yes -o StrictHostKeyChecking=accept-new \ -o UserKnownHostsFile=$keys/known_hosts \ $console_user@$console
# Wake the console: a serial line shows nothing until it receives something.sleep 1send -- "\r"
# ---- Reach a device prompt, whatever state the console was left in ----------set tries 0expect { -re $busy_re { fail "console port $port is in use by someone else" } -re {(?i)username:|login:} { send -- "$dev_user\r"; exp_continue } -re {(?i)password:} { send -- "$dev_pwd\r"; exp_continue } -re {(?i)(login incorrect|authentication failed|access denied)} { fail "device login failed on port $port" } -re {\(config[^)]*\)#} { send -- "end\r"; exp_continue } ;# left in config mode -re {--More--} { send -- "q"; exp_continue } ;# left in a pager -re $prompt_re {} timeout { incr tries if {$tries < 3} { send -- "\r"; exp_continue } fail "no response from the console on port $port" } eof { fail "console server closed the connection on port $port" }}
# ---- Collect -----------------------------------------------------------------send -- "$paging_off\r"expect -re $prompt_re
set timeout 240set raw ""send -- "$config_command\r"expect { -re {--More--} { append raw $expect_out(buffer); send -- " "; exp_continue } -re $prompt_re { append raw $expect_out(buffer) } timeout { fail "timed out reading '$config_command' on port $port" } eof { fail "connection closed while reading '$config_command'" }}
# ---- Log out so the port isn't left logged in --------------------------------set timeout 10send -- "exit\r"expect -re {(?i)username:|login:|press return} {} timeout {}closewait
# ---- Clean and print -----------------------------------------------------------regsub -all {--More--} $raw "" rawregsub -all {\x1b\[[0-9;]*[A-Za-z]} $raw "" rawregsub -all {[\x08\r]} $raw "" rawset lines [lrange [split $raw "\n"] 1 end-1]set config [string trim [join $lines "\n"]]if {$config eq ""} { fail "'$config_command' returned nothing on port $port" }
puts $configputs stderr "SIE-OK: console [llength $lines] lines via $console port $port"exit 0What gets stored
Only the command output, as in the Expect recipe:
hostname plant-sw-03interface FastEthernet0/1description PLC-line-2switchport access vlan 30Adapting it
- The username style of port access. Replace the
spawnline withspawn ssh -i ... $console_user:port[format %02d $port]@$consoleand pass the serial port number instead of the TCP port. - A device with no login on its console. Many OT devices drop straight to a prompt. The login lines are simply never matched.
- Menus instead of a CLI. Add the menu screens to the first
expectblock, as in the Expect recipe.