Skip to content

Instantly share code, notes, and snippets.

@pierric
Last active April 12, 2018 14:03
Show Gist options
  • Save pierric/d9eeea7528e23a4006758732abccdec5 to your computer and use it in GitHub Desktop.
Save pierric/d9eeea7528e23a4006758732abccdec5 to your computer and use it in GitHub Desktop.
Do some command for each address in the 'node' file
#! /bin/bash
includes=()
excludes=()
notMember () { for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 1; done; return 0; }
while getopts ":e:i:" opt; do
case $opt in
e)
excludes[${#excludes[@]}]=$OPTARG
;;
i)
if notMember $OPTARG ${includes[@]}; then
includes[${#includes[@]}]=$OPTARG
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit e
;;
esac
done
shift $((OPTIND-1))
#echo ${excludes[@]}
#echo $@
if [ -z "${includes[*]}" ]; then
nodes=$(cat nodes)
else
nodes=${includes[*]}
fi
for n in $nodes; do
if notMember $n ${excludes[@]}; then
cmd="ssh 192.168.2.$n '$@'"
echo ==== $cmd ====
exec "$cmd"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment