Write an SIE script in Bash
Bash is already on every rConfig server, and it’s the quickest way to run a few commands over SSH or glue command-line tools together. It’s a poor fit for anything that needs a password typed at a prompt, or real data handling.
Good for
- Read-only commands over SSH with key authentication.
- Wrapping a command-line tool (
dig,openssl,mysqldump, a vendor CLI) and cleaning its output.
Not good for: password logins (use Expect or Netmiko), JSON beyond simple jq use, or anything with much logic.
The command
/bin/bash {script_path}my-script.sh {device_ip} {device_username}A minimal script
#!/usr/bin/env bash## Minimal SIE script in Bash: run one read-only command over SSH with a key.## Usage: bash-skeleton.sh <ip> <username>
set -o pipefail
KEYS="/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys"CONFIG_COMMAND="show running-config"
fail() { echo "ERROR: $1" >&2; exit 1; }note() { echo "$1" >&2; }
HOST="${1:-}"; USER_NAME="${2:-}"[ -n "$HOST" ] && [ -n "$USER_NAME" ] || fail "usage: bash-skeleton.sh <ip> <username>"
note "collecting from $HOST"config=$(ssh -i "$KEYS/backup_ed25519" \ -o BatchMode=yes \ -o StrictHostKeyChecking=accept-new \ -o UserKnownHostsFile="$KEYS/known_hosts" \ -o ConnectTimeout=10 \ "$USER_NAME@$HOST" "$CONFIG_COMMAND") \ || fail "ssh to $HOST failed"
config=$(printf '%s\n' "$config" | tr -d '\r')[ -n "$(printf '%s' "$config" | tr -d '[:space:]')" ] || fail "no output from $HOST"
printf '%s\n' "$config"note "SIE-OK: bash $(printf '%s\n' "$config" | wc -l) lines from $HOST"Habits that matter in Bash
set -o pipefail. Without it,ssh ... | grep ...reports grep’s success even when ssh failed.- Capture, check, then print. Store the output in a variable, check it, and only then print it. Printing straight from a pipe sends half a config to stdout before you know the run failed.
- Quote every variable.
"$HOST", not$HOST. An empty or odd value otherwise changes the command. BatchMode=yeson everyssh. A key that isn’t accepted fails at once instead of waiting for a password until rConfig’s timeout.- Strip
\r. Many devices end lines with carriage returns, which show as changes on every line in a diff.
Recipes that use Bash
- Pre and post change snapshot
- Through a jump host
- DNS zones
- TLS certificates
- Not a network device (database schema, Linux files)