If you want to ssh
to multiple hosts in parallel, use a program like pdsh
(Parallel Distributed Shell).
For example, if your ip.txt
contains IP addresses instead of hostnames, or a mix of hostnames & IP addresses:
hosts="$(awk '{l = l","$0}; END {sub(/^,/,"",l); print l}' ip.txt)"
while pdsh -l ubuntu -w "$hosts" 'pgrep -f pattern' 2>/dev/null |
grep pattern ; do
sleep 10
done
This uses awk
to build a comma-separated list of IP addresses to connect to with ssh
.
If ip.txt
file contains only hostnames instead of IP addresses, it's a lot simpler:
while pdsh -l ubuntu -F ./ip.txt -a 'pgrep -f pattern' 2>/dev/null |
grep pattern ; do
sleep 10
done
Both of these assumes that ip.txt
has one IP address or hostname per line.
The while
loop runs until none of the hosts produce any output that matches the pattern. stderr from the pdsh
command is redirected to /dev/null to avoid spamming the terminal with error messages when the pgrep
command returns exit code 1 to pdsh
.
The only output is from the grep pattern
in the pipeline. Use grep -q pattern
if you want this silenced too.
ssh
from eating standard input with-n
or by closing stdin – thrig Feb 22 '18 at 17:5612684 13445 Process is running for XXXXXX 12684 13445 Process is running for XXXXXX 12684 13445 Process is running for XXXXXX 12684 13445 – Nani Feb 22 '18 at 18:00