I want to write a Bash-Script which loggs into several machines via ssh and first shows their hostname and the executes a command (on every machine the same command). The hostname and the output of the command should be displayed together. I wanted a parallel version, so the ssh-commands should be run in background and in parallel.
I constructed the bashscripted attached below.
The problem is: As the runonip
-function is executed in a subshell, it got no access to the DATA
-array to store the results. Is it somehow possible to give the subshell access to the Array, perhaps via a "pass by reference" to the function?
Code:
#!/bin/bash
set -u
if [ $# -eq 0 ]; then
echo "Need Arguments: Command to run"
exit 1
fi
DATA=""
PIDS=""
#Function to run in Background for each ip
function runonip {
ip="$1"
no="$2"
cmds="$3"
DATA[$no]=$( {
echo "Connecting to $ip"
ssh $ip cat /etc/hostname
ssh $ip $cmds
} 2>&1 )
}
ips=$(get ips somewhere)
i=0
for ip in $ips; do
#Initialize Variables
i=$(($i+1))
DATA[$i]="n/a"
#For the RunOnIp Function to background
runonip $ip $i $@ &
#Save PID for later waiting
PIDS[$i]="$!"
done
#Wait for all SubProcesses
for job in ${PIDS[@]}; do
wait $job
done
#Everybody finished, so output the information from DATA
for x in `seq 1 $i`; do
echo ${DATA[$x]}
done;