Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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. –  Gilles Mar 19 '13 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. –  frostschutz Mar 20 '13 at 0:55
why ssh bastion1 ssh ${s} df -h ? just use ssh ${s} df -h –  Rahul Patil Mar 20 '13 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). –  Awhitehatter Mar 20 '13 at 14:04
add comment

2 Answers

up vote 0 down vote accepted

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
share|improve this answer
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! –  Awhitehatter Mar 20 '13 at 18:34
add comment

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.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.