From c968b9be279af0433501fe7865124e20d90d0c04 Mon Sep 17 00:00:00 2001 From: Raine Date: Mon, 6 Nov 2023 17:57:32 +0100 Subject: [PATCH] feat: spread tool --- servers.txt | 5 ++++ tools/spread.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 servers.txt create mode 100755 tools/spread.sh diff --git a/servers.txt b/servers.txt new file mode 100644 index 0000000..a299d12 --- /dev/null +++ b/servers.txt @@ -0,0 +1,5 @@ +link +keymaker +apoc +kid +mouse diff --git a/tools/spread.sh b/tools/spread.sh new file mode 100755 index 0000000..5478236 --- /dev/null +++ b/tools/spread.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# spread command over multiple servers + +SPREAD_SERVERS=${SPREAD_SERVERS:-} +SPREAD_SERVERS_FILE=${SPREAD_SERVERS_FILE:-servers.txt} +SPREAD_USER=${SPREAD_USER:-$USER} +SPREAD_COMMAND=${SPREAD_COMMAND:-} + +while getopts "s:f:u:" opt; do + case $opt in + s) + SPREAD_SERVERS="$SPREAD_SERVERS $OPTARG" + ;; + f) + SPREAD_SERVERS_FILE="$SPREAD_SERVERS_FILE $OPTARG" + ;; + u) + SPREAD_USER="$OPTARG" + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + ;; + esac +done +shift $((OPTIND - 1)) + +if [ -n "$1" ]; then + SPREAD_COMMAND="$@" +fi + +if [ -f $SPREAD_SERVERS_FILE ]; then + SPREAD_SERVERS="$SPREAD_SERVERS $(cat $SPREAD_SERVERS_FILE)" +fi + +if [ -z "$SPREAD_SERVERS" ]; then + echo "No servers specified" + exit 1 +fi + +if [ -z "$SPREAD_COMMAND" ]; then + echo "No command specified" + exit 1 +fi + +_ssh() { + ssh \ + -l $SPREAD_USER \ + -o ConnectTimeout=5 \ + $1 \ + "$SPREAD_COMMAND" +} + +for server in $SPREAD_SERVERS; do + # while loop so we get:: $SPEAD_USER@$server: + case ${SPREAD_LOG_STYLE:-prefixed} in + prefixed) + _ssh $server | while read -r line; do + printf "%s@%-10s: %s\n" "$SPREAD_USER" "$server" "$line" + done + ;; + header) + printf "\n%s@%s\n---\n" "$SPREAD_USER" "$server" + _ssh $server + ;; + *) + _ssh $server + ;; + esac +done