ssh multiple servers

Hi folks.

I'm pretty new to unix, while I'm learning a lot I'm finding bash scripting quite confusing. Im sure it's not really, my head just hasn't clicked with it.

Anyway, I need a script to loop the ip addresses stored in a file and run a "pgrep <process>" and return the pid or some indicator that there wasn't one:

192.xxx.xxx.055 2513
192.xxx.xxx.056 10453
192.xxx.xxx.067 ---
etc.

Could some kind person please post me a script to do this and explain what's going on?

Many thanks.

------ Post updated at 10:30 AM ------

I should add, I do have ssh keys sent everywhere so passwords aren't a problem. :o)

You could try:

while read server <&3
do
  printf "%s: " "$server"
  ssh -q "$server" pgrep 'foo' 2>&1 || echo "Server not accessible or process not running"
done 3<serverfile

Where "foo" should be replace by the process pattern that you are looking for .

1 Like

The ssh command might NOT return the exit status of the last remote command.
Fix: check the exit status remotely.

ssh -qnx "$server" "
pgrep 'foo' 2>&1 || echo 'process not running'
" || echo "Server not accessible"