# ssh multiple servers

**URL:** <https://community.unix.com/t/ssh-multiple-servers/373928>\
**Category:** UNIX for Beginners Q & A\
**Created:** [October 11, 2018, 5:30am UTC](https://community.unix.com/t/ssh-multiple-servers/373928 "2018-10-11T05:30:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuntyScrunt](https://community.unix.com/letter_avatar/muntyscrunt/32/5_5575768a8748004e209b776fc1b2916d.png) [@MuntyScrunt](https://community.unix.com/u/MuntyScrunt)\
**Post date:** [October 11, 2018, 5:30am UTC](https://community.unix.com/t/ssh-multiple-servers/373928/1 "2018-10-11T05:30:48Z")

</div>

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:

```nohighlight
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)

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [October 11, 2018, 8:14am UTC](https://community.unix.com/t/ssh-multiple-servers/373928/2 "2018-10-11T08:14:39Z")

</div>

You could try:

```nohighlight
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 .

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [October 11, 2018, 11:28pm UTC](https://community.unix.com/t/ssh-multiple-servers/373928/3 "2018-10-11T23:28:41Z")

</div>

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

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

```
