WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script batch-command-runner

Batch Command Runner

Run one command on a list of SSH hosts, sequentially, with clear output.

category Automation shell Bash 4+ updated 2026-09-07
sshbatchautomationparallel
Safety notes
  • Runs commands on remote hosts. Requires key-based SSH access and explicit host authorisation.
  • The script runs hosts sequentially on purpose; use a configuration manager for large fleets.
commands involved: ssh

Dependencies: ssh

Usage

  • Create a hosts file with one hostname per line.
  • Pass the hosts file and the command to run.

Script

#!/usr/bin/env bash
set -euo pipefail

hosts_file="${1:-}"
shift || true
(($# > 0)) || { echo "usage: $0 HOSTS_FILE COMMAND [ARGS...]" >&2; exit 1; }
[[ -r "$hosts_file" ]] || { echo "cannot read $hosts_file" >&2; exit 1; }

command="$*"
fail=0

while IFS= read -r host; do
  [[ -z "$host" || "$host" == \#* ]] && continue
  echo "== $host =="
  if ssh -o BatchMode=yes -o ConnectTimeout=5 "$host" "$command"; then
    echo "-- ok"
  else
    echo "-- failed ($?)" >&2
    fail=1
  fi
done < "$hosts_file"

exit "$fail"

Example

printf '%s\n' web1 web2 db1 > hosts.txt
./batch-command-runner hosts.txt 'uptime && df -h /'

Notes

  • BatchMode=yes fails instead of prompting for a password, which is what you want in automation.
  • Quote the remote command as one argument so the local shell does not expand it.

Related scripts

SSH Hardening Audit

Inspect sshd configuration for common risky settings without changing anything.

Security