Skip to content

Instantly share code, notes, and snippets.

@dejanu
Created June 7, 2023 09:32
Show Gist options
  • Save dejanu/79197b6be8fc009529c3682f267a5306 to your computer and use it in GitHub Desktop.
Save dejanu/79197b6be8fc009529c3682f267a5306 to your computer and use it in GitHub Desktop.
Simple shell script that achieves concurrent SSH connections using background processes
#!/bin/bash
## #####################################################################
## run concurrently command passed as argv on multiple remote servers ##
## UPDATE: servers array and user variable ##
########################################################################
# define an array of remote servers
servers=("server1.fqdn" "server2.fqdn" "server3.fqdn")
# Function to execute command on a remote server
execute_command() {
server=$1
command=$2
# define USER for the ssh connection
ssh user@"$server" "$command"
}
for server in "${servers[@]}"
do
# exec arg command as background process
execute_command "$server" "$1" &
done
# wait for all background processes to complete
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment