I'm working on automating collecting disk space on a group of servers.

In a standard terminal, if I run:

ssh server1 ssh server2 df -h 

Then I get the output of the command, just fine.

However, if I attempt this method from within a script using a For Loop. Then it fails.

Example Script:


GROUP="server1 server2"

for s in $GROUP; do 
    ssh bastion1 ssh ${s} df -h

In the latter example, I get no output (or errors for that matter). I've tried various escaping and quoting, but still no luck...I have a feeling it's something blatantly obvious that I am overlooking. Any pointers?

  • 3
    Nothing wrong with the script you posted, it may be the way you're using it. Run bash -x /path/to/script and look at what it's doing. Mar 19, 2013 at 23:07
  • Even if it worked, you'd not usually do it that way. If you need to ssh to server1 and server 2 via bastion1, you'd make a connection to bastion1 once, then all others from there; not a new connection to bastion1 for every server. Also, there are existing solutions for monitoring servers, like munin. Mar 20, 2013 at 0:55
  • why ssh bastion1 ssh ${s} df -h ? just use ssh ${s} df -h Mar 20, 2013 at 2:33
  • Thanks for the comments, it's necessary to hit the bastion host for remote access. There is no VPN, the bastion host has a public IP and from there I can jump to internal hosts. I am unaware of a method that would allow me to open a session and interact with that session remotely (certainly open to suggestions). Mar 20, 2013 at 14:04

2 Answers 2


You have two Method

Method 1:

Groups="server1 server2"
echo $Groups | xargs -n1 |  xargs  -I'{}' echo ssh '{}' df

Note:- if output is fine of above command then just remove echo in last then it will run commands on remote servers. do not use variable in CAPS because it's use for environment variable

Method 2:

You can refer below script

# ssh password less login is required

# mention below remote server seperated by space

remote_srv=( )

do_ssh() {
    ssh $s "$@"
    echo -e

header() {
    echo "#==================={$@}==========================#"

n=${#remote_srv[@]} # number of ip's in array

for (( i=0;i<$n;i++)); do
            echo -e
            echo "$(tput bold)$(tput setaf 2)* Connected Server: ${remote_srv[$i]}  @@ $(date) @@"
            echo "* Fetching info...$(tput sgr 0)"

            header "Memory Usage"
            do_ssh free -m

            header "Disk Usage"
            do_ssh df -h

## EOF
  • Thank you @Rahul! The all caps variable is a tip I didn't know. Also both functions and array in BASH were new to me. This worked, I ended putting the script on the bastion host and then I just call it remotely. Thanks for the input! Mar 20, 2013 at 18:34

UNIX has a better solution for you, you don't need to ssh and run df or du or etc ....

you can use tmux command on server and close sessions, But you didn't close your programs if you open via tmux.tmux works such as screen command but very very complex and useful.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .