Skip to content

Instantly share code, notes, and snippets.

@alekratz
Created December 22, 2015 23:28
Show Gist options
  • Save alekratz/4104e5b82895b994b21f to your computer and use it in GitHub Desktop.
Save alekratz/4104e5b82895b994b21f to your computer and use it in GitHub Desktop.
#!/bin/bash
# This be a script that sends a command via SSH to a list of hosts
function usage() {
echo "usage: $0 [ -p password -u username ] command"
}
# I love bash
#hosts=$(sed -r '/^127.0.0.1.*$|^::1.*$|^#.*$|^\s*$/d'</etc/hosts | awk '{print $2}')
hosts="node1 node2 node3 node4"
# getopts
while getopts ':p:u:' flag; do
case $flag in
p)
pass=$OPTARG
;;
u)
user=$OPTARG
;;
esac
done
shift $((OPTIND-1))
if [[ $user == "" ]]; then
user=hadoop
fi
if [[ $pass == "" ]]; then
pass=appState
fi
if [[ $1 == "" ]]; then
usage
exit 1
else
command="$1"
fi
for h in $hosts; do
#echo sshpass -p $pass ssh $user@$h "$command"
sshpass -p $pass ssh $user@$h "$command"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment