2

I have 20 machines, each running a process. The machines are named:

["machine1", "machine2", ...., "machine20"]

To inspect how the process is doing on machine1, I issue the following command from a remote machine:

ssh machine1 cat log.txt

For machine2, I issue the following command:

ssh machine2 cat log.txt

Similarly, for machine20, I issue the following command:

ssh machine20 cat log.txt

Is there a bash command that will allow me to view the output from all machines using one command?

1
  • the folks at ServerFault might be more apt to answer this question (I've voted for migration.) Commented Mar 2, 2011 at 17:12

7 Answers 7

3

If the machines are nicely numbered like in your example:

for i in {1..20} ; do ssh machine$i cat log.txt; done

If you have the list of machines in a file, you can use:

cat machinesList.txt | xargs -i ssh {} cat log.txt
Sign up to request clarification or add additional context in comments.

Comments

3

You could store all your machine names in an array or text file, and loop through it.

declare -a machineList=('host1' 'host2' 'otherHost') # and more...


for machine in ${machineList[@]}
do
    ssh $machine cat log.txt
done

I assume your machines aren't literally named 'machine1', 'machine2', etc.

Some links:

2 Comments

The only problems I see with this approach is: 1: what happens if the command is really long? 2.) What happens of one of the machines in machineList is down or non responsive?
@ajpyles - 1. If the command is really, really long, then perhaps expressing it with my method isn't the best way to do it. It depends on what use case is, I didn't want to present some general solution, I gave a quick solution that the asker could use without much problem. 2. If a machine is unresponsive, the ssh command will return an error and the script will progress. You could also launch ssh asynchronously if you wanted. There could be other problems like if the remote ssh box wants a username/password, but in this scenario it seems as though the asker has that part figured out.
2
for i in {1..20}
do
   ssh machine$i cat log.txt
done

Comments

2

Use a loop?

for i in {1..20}
do
   ssh machine$i cat log.txt
done

But note that you're running cat within a remote shell session, not the current one, so this might not quite work as you expect. Try it and see.

1 Comment

nice we have the same loop in the same minute
2

Put your hosts in a file and use a while loop as shown below. Note the use of the -n flag on ssh:

while read host; do ssh -n $host cat log.txt; done < hosts-file

Alternatively you can use PSSH:

pssh -h hosts-file -i "cat log.txt"

Comments

2

I would recommend using a program called Shmux. Despite the name, it works really well. I've used it with more than 100 machines with good results. It also gracefully handles machine failures for you which could be a disadvantage with a bash for loop approach.

I think the coolest thing about this program is the ability to issue multiple threads for your commands which allows to run the commands on all 20 machines in parallel.

Comments

1

Aside from the suggestions for using a loop, you might want to take a look at tools, like pssh or dsh, designed for running commands on multiple clients.

1 Comment

+1 Interesting to know for when I have to deal with 20 new instances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.