Created
April 6, 2021 13:49
-
-
Save rafamdr/1306b19f98baeb6b9638d26b49c14fcb to your computer and use it in GitHub Desktop.
Execute commands in many servers through ssh using sshpass (with password)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT | |
if [ ! "$BASH_VERSION" ] ; then | |
exec /bin/bash "$0" "$@" | |
fi | |
SERVERS=$(cat <<EOF | |
10.0.0.1 | |
10.0.0.2 | |
EOF | |
) | |
echo -n "SSH User": | |
read -r sshusertemp | |
echo -n "SSH Password": | |
read -sr sshpasswordtemp | |
printf "\n" | |
for srv in $SERVERS | |
do | |
echo "Running command in: $srv ..." | |
(sshpass -p "${sshpasswordtemp}" ssh -o StrictHostKeyChecking=no "${sshusertemp}"@"${srv}" \ | |
"date && ls -lah /var/logs" > "./output_${srv}.txt") & | |
asg_pids="${asg_pids} $!" | |
done | |
printf "\nWaiting all tasks ending or a keyboard CTRL+C...\n" | |
for pid in $asg_pids; do | |
wait "${pid}" || (echo "Task ${pid} failed!") | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment