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 & utilities

Kept in this browser · My desk

Connect
Log in

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.

Device

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

Commands
Timing, output and logout
The script (97 lines)
#!/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.
How to run it

expect login-and-run.exp Set DEVICE_PASSWORD first.

  • 0 done
  • 1 the login failed (no prompt in time, the connection closed, or a password refused)
  • 2 missing or wrong input (an unset variable, a bad host argument, no password typed)
  • 3 a command did not finish (silence longer than the command timeout, a question nobody answered)
  • 4 the session closed while a command or enable was running
The script, part by part
  1. lines 1 to 7 What the script does, how to run it, where its secrets come from, and its exit statuses.
  2. lines 9 to 10 The timeout for each login step, set before the first expect so no expect runs on the default of 10 seconds.
  3. lines 12 to 15 The host (fixed, or from the command line and checked) and the user.
  4. lines 17 to 24 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.
  5. lines 26 to 27 log_user 0: the login stays off the screen, so nothing a device echoes there can be seen.
  6. lines 29 to 30 The connection: ssh with StrictHostKeyChecking=accept-new (and BatchMode with a key), telnet, or the program.
  7. lines 32 to 53 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.
  8. lines 55 to 72 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.
  9. lines 74 to 75 The timeout for each command, at the top level, after the login.
  10. lines 77 to 78 The profile's commands that turn paging off, each waited for like any command.
  11. lines 80 to 81 From here on the session goes to the screen, to the log file, or both; the file is opened only now.
  12. lines 83 to 85 Your commands, each sent as typed (Tcl's special characters protected).
  13. lines 87 to 97 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.

API endpointGEThttps://ronutz.com/api/v1/expect-script-builderDocumented, not served. Opens the specification.

References

Appears in these workflows

Who uses this