Expect script builder
Choose ssh, telnet or another program, how to log in, a device profile (Cisco IOS XE, F5 BIG-IP tmsh, FortiOS, Extreme Switch Engine and Fabric Engine, or your own prompt) and the commands, and get a complete Expect script that logs in, deals with paging, runs them and logs out, changing nothing on the device unless you ask (on FortiOS it answers --More-- rather than change a global setting). No password is ever written into it, nothing secret reaches a log, ssh checks host keys with accept-new, and every script it writes passes the Expect script explainer with no error and no warning.
Text & utilitiesKept in this browser · My desk
The script reads the password from the environment, checks that it is there, and removes it from the environment before starting ssh, telnet or the program.
Prompts, paging and enable from Cisco IOS XE 17.x System Management Configuration Guide; Cisco IOS Configuration Fundamentals Command Reference (IOS and IOS XE), read on 2026-10-27. Expect against network devices
#!/usr/bin/expect -f
# Written by the Expect script builder (ronutz.com/tools/expect-script-builder).
# Logs in to Cisco IOS XE (the IOS command line) over ssh, runs 2 commands and logs out.
# Device profile: cisco-ios, from the vendor's documents read on 2026-10-27.
# Run it with: expect <this file>
# It reads DEVICE_PASSWORD from the environment; nothing secret is written in this file.
# Exit status: 0 done; 1 the login failed; 2 missing or wrong input; 3 a command did not finish; 4 the session closed early.
# Wait up to 30 seconds for each step of the login.
set timeout 30
# The device.
set host "sw1.example.net"
# The account.
set user "netops"
# The password comes from the environment variable DEVICE_PASSWORD, never from this file.
if {![info exists env(DEVICE_PASSWORD)] || $env(DEVICE_PASSWORD) eq ""} {
send_user "Set DEVICE_PASSWORD in the environment first.\n"
exit 2
}
set password $env(DEVICE_PASSWORD)
# Take it out of the environment, so the programs this script starts do not inherit it.
unset env(DEVICE_PASSWORD)
# Keep the login off the screen, so nothing a device echoes there can be seen.
log_user 0
# Connect. accept-new records the key of a host met for the first time and refuses a host whose key has changed.
spawn ssh -o StrictHostKeyChecking=accept-new -l $user $host
# Log in: answer the password question once, then wait for the prompt.
set login_tries 0
expect {
-nocase -re {password: ?$} {
if {$login_tries > 0} {
send_user "The password was refused.\n"
exit 1
}
incr login_tries
send -- "$password\r"
exp_continue
}
-re {(?:^|[\r\n])[^\r\n#>]+[>#] ?$} { }
timeout {
send_user "No prompt from $host within $timeout seconds.\n"
exit 1
}
eof {
send_user "The connection to $host closed before the prompt: the login was refused, the host key has changed, or the host cannot be reached.\n"
exit 1
}
}
# Send one command line, then wait for the prompt that follows its output.
# Each line of output restarts the timer, so the timeout counts silence, not the whole command.
proc run_command {command} {
global host timeout
send -- "$command\r"
expect {
-re {(?:^|[\r\n])[^\r\n#>]+[>#] ?$} { }
-re {[\r\n]} { exp_continue }
timeout {
send_user "\n\"$command\" did not finish: nothing more came from $host for $timeout seconds.\n"
exit 3
}
eof {
send_user "\n$host closed the session during \"$command\".\n"
exit 4
}
}
}
# From here on, each command may stay silent for up to 60 seconds.
set timeout 60
# Turn paging off for this session.
run_command "terminal length 0"
# Show the session from here on.
log_user 1
# The commands.
run_command "show version"
run_command "show ip interface brief"
# Log out, and wait for the device to close the connection.
send -- "exit\r"
expect {
eof { }
timeout {
send_user "$host did not close the session; closing it.\n"
close
}
}
wait
exit 0
The Expect script explainer reads it here: 0 errors, 0 warnings, 0 notes. Open the explainer
- Set DEVICE_PASSWORD in the environment of the shell that runs the script; the script checks it and removes it from the environment before starting the connection.
expect login-and-run.exp Set DEVICE_PASSWORD first.
0done1the login failed (no prompt in time, the connection closed, or a password refused)2missing or wrong input (an unset variable, a bad host argument, no password typed)3a command did not finish (silence longer than the command timeout, a question nobody answered)4the session closed while a command or enable was running
- What the script does, how to run it, where its secrets come from, and its exit statuses.
- The timeout for each login step, set before the first expect so no expect runs on the default of 10 seconds.
- The host (fixed, or from the command line and checked) and the user.
- The password from the environment: present and not empty, or the script stops; then removed from the environment so no program the script starts inherits it.
- log_user 0: the login stays off the screen, so nothing a device echoes there can be seen.
- The connection: ssh with StrictHostKeyChecking=accept-new (and BatchMode with a key), telnet, or the program.
- One expect for the whole login: each question is answered once (a second one means it was refused), the prompt ends it, and timeout and eof stop the script with status 1.
- run_command: send one line with a return, then wait for the prompt; each line of output restarts the timer (exp_continue), so the timeout counts silence.
- The timeout for each command, at the top level, after the login.
- The profile's commands that turn paging off, each waited for like any command.
- From here on the session goes to the screen, to the log file, or both; the file is opened only now.
- Your commands, each sent as typed (Tcl's special characters protected).
- The logout command and the end of the connection (or a close), then wait, so the process is reaped.
The builder writes the script in your browser and never runs it. It does not answer questions a command asks, does not handle a prompt the profile's pattern misses (a banner or a command line that ends in > or #, for example), and writes comments and messages in English, like the commands themselves. Run the script against a lab device first.
References
- expect(1), Expect 5.45.4 manual source (core.tcl-lang.org, tag expect-5-45-4)
- expect.c, Expect 5.45.4 (core.tcl-lang.org, tag expect-5-45-4)
- OpenSSH ssh(1) (man.openbsd.org, dated 9 October 2026)
- OpenSSH ssh_config(5): StrictHostKeyChecking, BatchMode, IdentitiesOnly (man.openbsd.org, dated 9 October 2026)
- RFC 854, Telnet Protocol Specification (May 1983)
- The Expect suite's device profiles (src/content/expect/device-profiles.ts): Cisco IOS XE 17.x, F5 TMSH Reference v17, FortiOS 7.6.8, Switch Engine 33.7.1, Fabric Engine 9.4