w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
shell script to ssh remote machines and print the output of the top command

When you do

ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out"

you’re writing the output to ~/mysh/top_out on the remote host, not the local machine. The remote host might not be using the same physical home directory as your local machine. If you have NFS or something sharing your home directory on some machines but not all, then you’d see the symptoms you described.

Try doing

ssh host$i "top -n1 -b | head -n 15" >> ~/mysh/top_out

instead, or to make things slightly cleaner, maybe even

#!/bin/bash

for i in $(seq 1 8); do
    (echo "host$i"
     ssh host$i "top -n1 -b | head -n 15") >> ~/mysh/top_out
    echo "done host$i"
done




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.