while loop to ssh only take first element

24 views Asked by At

I look for getting uname -a on a list of VMs:

$ ssh srv-13 uname -a
Linux srv-13 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2 (2019-08-28) x86_64 GNU/Linux
$

$ ssh akd4 uname -a
Linux akd4 4.19.0-22-amd64 #1 SMP Debian 4.19.260-1 (2022-09-29) x86_64 GNU/Linux
$ 

I want to loop on them with a while, it work well:

$ echo 'akd4 srv-13'| tr ' ' '\n'
akd4
srv-13
$

$ echo 'akd4 srv-13'| tr ' ' '\n' |while read i;do echo "==>$i";done
==>akd4
==>srv-13
$

But when I try to loop with the uname -a through ssh I only get the first one:

$ echo 'akd4 srv-13'| tr ' ' '\n' |while read i;do echo "==>$i"; ssh $i uname -a;done
==>akd4
Linux akd4 4.19.0-22-amd64 #1 SMP Debian 4.19.260-1 (2022-09-29) x86_64 GNU/Linux
$ 

Obviously I know how to loop with for:

$ 
$ for i in akd4 srv-13;do ssh $i uname -a  ;done
Linux akd4 4.19.0-22-amd64 #1 SMP Debian 4.19.260-1 (2022-09-29) x86_64 GNU/Linux
Linux srv-13 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2 (2019-08-28) x86_64 GNU/Linux
$ 

My question is to understand why I don't get the expected below ?:

$ echo 'akd4 srv-13'| tr ' ' '\n' |while read i;do echo "==>$i"; ssh $i uname -a;done
==>akd4
Linux akd4 4.19.0-22-amd64 #1 SMP Debian 4.19.260-1 (2022-09-29) x86_64 GNU/Linux
==>srv-13
Linux srv-13 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2 (2019-08-28) x86_64 GNU/Linux
$
0

There are 0 answers