Stop your scripts from outrunning slow network gear
If you have ever pasted a config into a console and watched the device fall behind, you already know the failure mode. The script has moved on. The router is still printing --More--, or it swallowed half a line and now says % Invalid input. On a USB serial console it is even easier to lose: there is often no flow control, and a 9600 baud CLI cannot keep up with a paste that was generated at full speed.
The fix is not "type slower and hope." It is two separate ideas:
- Prompt-gated sends. Do not send the next command until the device has finished the last one and returned a prompt.
- Pacing. When you must paste, wait between lines or characters, or wait until the device echoes the line you just sent.
Those rules apply to any tool. The worked examples below use JackedIN's exec command because that is the CLI I work on, and because its documented behavior matches this model exactly. Use them on lab gear you are allowed to test. Addresses in this article are documentation placeholders (192.0.2.10, switch1, router1).
Why a pipe is the wrong default
A common shortcut looks like this:
printf '%s\n' 'show running-config' 'show vlan' | ssh lab@192.0.2.10
Or the same idea piped into a terminal program's connect command. That often appears to work for a single short show command. It is a bad way to change configuration, for a mechanical reason: end-of-file on standard input is treated as the end of the session. The device may still be paging. It may have printed an error. The script has already exited.
JackedIN's own user's guide is blunt about this: do not pipe a command into jackedin connect (or into plink) and assume the change landed. A pipe into an interactive SSH or Telnet session also closes the session when the pipe ends, so it is the wrong tool for a config change.
Prompt-gated sends
jackedin exec is built for the "read the list, then drive one session" case. It reads every command first, from arguments, --file, or stdin. Then it opens one persistent interactive session and sends the next command only after the device prompt returns. It also pages through --More--, ---(more)---, ---(more 57%)---, ---(more 100%)---, and Press any key, instead of leaving you stuck behind a pager.
Set the password in the environment rather than on the command line (JACKEDIN_PASSWORD is used when --password and --password-stdin are omitted):
# password already in JACKEDIN_PASSWORD; it is not printed
jackedin exec --host 192.0.2.10 --user lab --trust-host-key \
--file commands.txt
A short commands.txt for a lab switch might be:
show version
show vlan brief
--trust-host-key accepts a new host key. It does not accept a changed host key. That is the behavior you want in a script: a host you have never seen can be recorded, but a key that suddenly does not match should stop the run.
If the prompt JackedIN detects is wrong for that box, --prompt replaces it with a regex you supply. --vendor ios (or junos, nxos, arista, mikrotik, and the other documented profiles) forces a device profile, including the command that turns paging off. If you omit --vendor, the prompt line or a banner such as JUNOS or NX-OS selects the profile.
The process exits non-zero if any command, timeout, or verify check fails. That is what you want in a script: a missed prompt should be a failed run, not a silent half-applied config.
One more safety rule from the same command: if the session returns to login:, Username:, or Password:, later commands are not sent. Expect checks are then scored against output already captured. They are not typed at the login prompt. That stops a dropped session from turning the rest of your file into a username or a password.
Pacing, when the prompt alone is not enough
Waiting for the prompt covers most SSH sessions. Serial consoles, and some Telnet paths, still need a pause. A device that echoes slowly, or a console with no flow control, can drop characters in the middle of a line even when you waited for switch1#.
exec gives you three pacing knobs, plus an explicit sleep:
-
--line-delay MSwaits that many milliseconds between lines. -
--char-delay MSwaits between characters. -
--wait-for-echowaits until the device echoes a line before the next one is sent. -
!sleep 500in the command list waits 500 milliseconds and sends nothing.
On serial, if you omit --line-delay, exec uses a 100 ms line delay. The next command is still never sent until the prompt returns. So a USB console session from Windows looks like this:
jackedin ports
jackedin exec --protocol serial --host COM5 --baud 9600 \
--wait-for-echo --file commands.txt
On Linux, the same session is --serial /dev/ttyUSB0 (or --host /dev/ttyUSB0). Opening the port requires membership in the dialout group on many distributions (uucp or lock on some others): add your user, then log out and back in. Defaults are 9600 8N1 with no flow control.
A command file that needs an extra pause after a write can include the sleep as its own line:
configure terminal
hostname router1
end
write memory
!sleep 500
show running-config
!break in that list sends a break on the session, which is a different tool (for example, a ROMMON recovery step). It is not a substitute for pacing.
See the plan, then prove the result
Before you send a change, print the plan and send nothing:
jackedin exec --host 192.0.2.10 --user lab --trust-host-key \
--dry-run --file commands.txt
--dry-run does not connect. Use it in CI or at the desk when you want to read the list one more time.
After a real run, do not trust "the command was sent." Ask for a string that should be in the output. --expect-file adds YAML checks on top of --verify and --expect-match. A one-shot check can be:
printf '%s\n' 'configure terminal' 'hostname router1' 'end' |
jackedin exec --host 192.0.2.10 --user lab --trust-host-key \
--verify 'show run' --expect-match 'hostname router1'
If hostname router1 is not in the verify output, the process fails. That is a better signal than a zero exit from a paste that the device never fully accepted.
A small checklist
- Do not use a stdin pipe into an interactive
connectorsshsession to change configuration. Read the commands, then drive one session that waits for a prompt. - Let the tool page through
--More--instead of racing the pager. - Add
--line-delay,--char-delay, or--wait-for-echoon serial and on any box that drops characters. - Use
!sleepwhen you need a pause that is not tied to a prompt, such as afterwrite memory. - Stop the list if the session falls back to a login prompt.
- Dry-run the plan, then verify a string you expect to see.
The same prompt-gated exec path is also how JackedIN's Python client and the local API run commands, but you do not need those to get the benefit. A command file and jackedin exec are enough to stop a script from outrunning the gear.
I built JackedIN, an SSH, Telnet, and serial terminal with a CLI for this kind of work. There is a 3-day trial with no account or card. Docs and downloads: https://getpathprobe.com/jackedin/
Top comments (0)