Reach a device through a jump host
Some networks can only be reached through a jump host (also called a bastion): you log in to it first, and connect onwards from there. This recipe keeps the jump host details in one SSH config file in the scripts folder, so every script that uses it reaches the right devices through the right jump host without any of that in the rConfig command.
When to use it
- rConfig can reach the jump host, but not the devices behind it.
- Putting a Vector agent inside that network isn’t an option. Where it is, Vector is usually the better fix: the agent collects locally and no jump host is involved.
What you need
- An SSH key that the jump host and the devices both accept, or one key for each.
- The jump host’s own host key, and the devices’ host keys, in the scripts
keys/known_hostsfile.accept-new(below) records them on first contact. AllowTcpForwardingpermitted on the jump host for the account you use. SSH’sProxyJumpneeds it.
The SSH config file
Save this as keys/ssh_config in the scripts folder, owned by the web user and mode 0600. Each Host block matches device addresses and names the jump host to go through:
# Devices in the plant network are reached through jump-plant.Host 198.51.100.* ProxyJump backup@jump-plant.example.com User backup
# The jump host itself.Host jump-plant.example.com User backup
# Settings for every connection made with this file.Host * IdentityFile /var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys/backup_ed25519 UserKnownHostsFile /var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys/known_hosts StrictHostKeyChecking accept-new BatchMode yes ConnectTimeout 10ssh -F uses only this file and ignores the web user’s own SSH settings, so a scheduled run and your test run behave the same.
The template
main: name: "SIE - Through a jump host" desc: "Collects a config over SSH through a jump host"
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: jump"The command
/bin/bash {script_path}jump-host-backup.sh {device_ip}The username comes from the SSH config file, so it isn’t on the command line.
The script
Save it as jump-host-backup.sh in the scripts folder.
#!/usr/bin/env bash## Collect a config over SSH through a jump host, for the rConfig Script# Integration Engine. The jump host and key come from keys/ssh_config.## Usage: jump-host-backup.sh <device_ip>## The config goes to stdout. Progress, errors and the success marker go to# stderr. Exit code 0 on success, 1 on any failure.
set -o pipefail
KEYS="/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys"SSH_CONFIG="$KEYS/ssh_config"CONFIG_COMMAND="show running-config"
fail() { echo "ERROR: $1" >&2 exit 1}
HOST="${1:-}"[ -n "$HOST" ] || fail "usage: jump-host-backup.sh <device_ip>"[ -r "$SSH_CONFIG" ] || fail "cannot read $SSH_CONFIG"
echo "collecting from $HOST through the jump host in $SSH_CONFIG" >&2
errfile=$(mktemp) || fail "cannot create a temporary file"trap 'rm -f "$errfile"' EXIT
if ! output=$(ssh -F "$SSH_CONFIG" "$HOST" "$CONFIG_COMMAND" 2>"$errfile"); then fail "ssh to $HOST failed: $(tail -n 1 "$errfile")"fi
output=$(printf '%s\n' "$output" | tr -d '\r' | grep -Ev '^(Building configuration|Current configuration :|! Last configuration change|! NVRAM config last updated)')
[ -n "$(printf '%s' "$output" | tr -d '[:space:]')" ] || fail "'$CONFIG_COMMAND' returned nothing from $HOST"
printf '%s\n' "$output"echo "SIE-OK: jump $(printf '%s\n' "$output" | wc -l) lines from $HOST" >&2exit 0Test the path by hand
As the web user, check each hop on its own before involving rConfig:
SCRIPTS=/var/www/html/rconfig8/current/storage/app/rconfig/scriptssudo -u apache ssh -F $SCRIPTS/keys/ssh_config jump-plant.example.com true && echo "jump host OK"sudo -u apache ssh -F $SCRIPTS/keys/ssh_config 198.51.100.10 "show version" | head -3If the first works and the second doesn’t, the problem is between the jump host and the device, not in rConfig.
Adapting it
- Several jump hosts. Add a
Hostblock per network, each with its ownProxyJump. One script serves them all. - Devices that need a password or an interactive session. Use the Expect recipe and change its
spawn sshline tospawn ssh -F $ssh_config $user@$ip, or use Netmiko withssh_config_fileset to this file. Netmiko’s SSH library readsProxyCommandlines but notProxyJump, so for Netmiko write the jump asProxyCommand ssh -F <this file> -W %h:%p jump-plant.example.com. - Devices that reject a command on the SSH line. Some platforms only accept commands typed into an interactive session. For those, use Expect or Netmiko as above.