'Executing a command via SSH during a while loop

I have a list of servers in a file I'll call "boxes."

I'm running the following:

bastion:~ # while read i j; do ssh -n $i 'echo $(hostname)'; done < boxes

When I run this, I should be getting the host name of every box I'm supposedly SSHing into right? But what I'm getting is my hostname "bastion" over and over as if the command is not SSHing to any box.

Any idea why that might be or what I'm doing wrong here?

Thanks.

EDIT: I couldn't get SSH to work for this task. In the end I swapped the ssh for ansible -m shell.



Solution 1:[1]

hostname is enough

my favorite solution with GNU Parallel (much faster) sudo apt-get install parallel

 parallel --nonall --slf boxes hostname

you can add --tag and much more https://www.gnu.org/software/parallel/parallel_tutorial.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lojza