Skip to content

Instantly share code, notes, and snippets.

@rotanid
Created September 2, 2018 18:12
Show Gist options
  • Save rotanid/0c369c7e71f3320b901dcc0cc4bf0039 to your computer and use it in GitHub Desktop.
Save rotanid/0c369c7e71f3320b901dcc0cc4bf0039 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 hostfile cmdfile"
echo " hostfile contains a list of hostnames or IP addresses"
echo " cmdfile contains a list of commands that should be run on the hosts"
exit 1
fi
HOSTLIST="$1"
CMDLIST="$2"
if [ ! -e "$HOSTLIST" ] || [ ! "$(wc -l < $HOSTLIST)" -ge 1 ]; then
echo "problem with the supplied hostlist, $HOSTLIST, aborting"
exit 1
fi
if [ ! -e "$CMDLIST" ] || [ ! "$(wc -l < $CMDLIST)" -ge 1 ]; then
echo "problem with the supplied list of commands, $CMDLIST, aborting"
exit 1
fi
declare -a HOSTARR
declare -a CMDARR
readarray -t HOSTARR < $HOSTLIST
readarray -t CMDARR < $CMDLIST
CMDS="$(IFS=';' ; echo "${CMDARR[*]}")"
for HOST in "${HOSTARR[@]}"; do
echo -e "\n> $HOST\n"
ssh root@$HOST "$CMDS"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment