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
That sounds overly complicated way to SSH somewhere and call a command.
Just type:
Voila. :)
Edit 1
Example of remote output parsing:
Outputs comma separated list of mountpoint,availablespace like:
This may not be the most efficient or elegant way, but how about something like:
--perform your backup--
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 ?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 fileout.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
Contents of script.sh
This runs in the local machine