0

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
$
user3313834
  • 7,327
  • 12
  • 56
  • 99
  • Better use `ansible` and ad-hoc mode – Gilles Quénot Feb 19 '23 at 08:39
  • `ssh` reads its input, which steals the rest of the server list. See ["While loop stops reading after the first line in Bash"](https://stackoverflow.com/questions/13800225/while-loop-stops-reading-after-the-first-line-in-bash), [BashFAQ #89: "I'm reading a file line by line and running ssh or ffmpeg, only the first line gets processed!"](http://mywiki.wooledge.org/BashFAQ/089), and many previous questions. – Gordon Davisson Feb 19 '23 at 09:05

0 Answers0