2

I have a script that backs-up remote systems, and want it to display disk space on the remote backup device prior and post running backup script.

Thanks to another post have learnt how to run remote commands via SSH such as (SSH keys have been setup for auto login).

echo `ssh -t user@host uname -a`

However, how can I get a multi line response that comes from a command such as

echo `ssh -t user@host df`

Response just shows the last line of output from df

1
  • 1
    mattias is right; this is useless use of echo (and backticks, besides). :) if you post your whole script, we can probably advise on a better way to do it. (hint: store command output in variables to display later)
    – darkuncle
    Commented Jan 7, 2012 at 0:13

4 Answers 4

7

That sounds overly complicated way to SSH somewhere and call a command.
Just type:

ssh user@host df

Voila. :)

Edit 1
Example of remote output parsing:

ssh user@host df -P | tail -n +2 | awk '{print $6 "," $4}'

Outputs comma separated list of mountpoint,availablespace like:

/,1393276
/usr,3524132
/var,2560004
5
  • Sorry. I should have noted that command is to go into a script that runs backups and currently shows disk space on local host prior and post backup. However I also want to display disk space on the remote backup device prior and post running backup script. Commented Jan 7, 2012 at 0:10
  • Well, its still the same basically. You can just put it inside a script, parse the output like from any other command, etc. So this is still the right thing to do. :) Commented Jan 7, 2012 at 0:31
  • You are right. I have adapted your suggestion using grep to get exactly what I needed. Commented Jan 7, 2012 at 1:33
  • Ah. Thank you for explaining that @MadHatter. I have done as you suggested. Commented Jan 8, 2012 at 10:38
  • Thanks, Eureka: it's appreciated by the whole community.
    – MadHatter
    Commented Jan 8, 2012 at 13:18
1

This may not be the most efficient or elegant way, but how about something like:

REMOTEDF="ssh username@hostname df"

echo "Pre-backup df from hostname"
echo date >> $HOME/df-backups.out
echo "" >> $HOME/df-backups.out
$REMOTEDF >> $HOME/df-backups.out
echo "" >> $HOME/df-backups.out

--perform your backup--

echo "Backup of hostname is complete; post-backup df:"
echo date >>
$HOME/df-backups.out
echo "" >> $HOME/df-backups.out
$REMOTEDF >> $HOME/df-backups.out
echo "" >> $HOME/df-backups.out

1
  • Thank you all for suggestions. I have been able to adapt the remote output parsing command from Mattias to do exactly what I need. Commented Jan 7, 2012 at 1:31
0

here echo is the source of trouble. Remove the echo and you will get mutiline output. Why not just do ssh username@hostname cmd Why -t ?

0

Here's my hacky solution. This code is not perfect and I'm sure that there are other elegant and efficient methods than this. But here goes.

I'm going to create a cmds.txt file with the input commands to run on the remote server and the output from the remote server will be saved in another file out.txt.

By separating the input command in a separate file, it will become easier to modify code.

Also, saving the output to a text file, will preserve the output by preserving line breaks; Helpful when we need to fetch multi-line output from the remote server.

Contents of cmds.txt

Code placed here will be executed in remote server
df

Contents of script.sh

This runs in the local machine
#!/bin/bash
cmd=$(cat cmds.txt)
/usr/bin/ssh -o StrictHostKeyChecking=no ssh -i $KEY $USERNAME@$HOST "$cmd" > out.txt


cat out.txt

You must log in to answer this question.

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