Read a file of hostnames and run `uptime` on each over SSH in parallel, max 10 at a time.
AdvancedHands-onBash scripting tasks
Answer
xargs -a hosts.txt -P 10 -I{} \
ssh -o BatchMode=yes -o ConnectTimeout=5 {} 'echo "{}: $(uptime)"' 2>&1
Without xargs, using Bash job control:
while read -r host; do
ssh -n -o BatchMode=yes -o ConnectTimeout=5 "$host" uptime | sed "s/^/$host: /" &
while (( $(jobs -rp | wc -l) >= 10 )); do wait -n; done
done < hosts.txt
wait
Key trick → ssh -n (or BatchMode with xargs) stops ssh from swallowing the rest of stdin, which is why naive while read + ssh loops only process the first host.
More on Bash scripting tasks
Write the safe header you start every Bash script with, and explain each part.BeginnerWrite a script that alerts when any filesystem is above 80% usage.BeginnerParse an Nginx access log and print the top 10 client IPs by request count.BeginnerWrite a backup script that tars a directory with a timestamp and keeps only the last 7 backups.IntermediateWrite a health-check loop that polls a URL until it returns 200, with a timeout.IntermediateParse command-line flags `-e <env>`, `-v` (verbose) and `-h` in a Bash script.Intermediate