Team, am executing some commands on remote ubuntu server from ubuntu client. I want to save output of that to a local client directory.

Am trying to run other commands from client after I save the result of command that i ran over ssh session.

Steps: Connect to Remote ssh over proxycommand, Then start a process, then save its state in a file. I want this file to be saved locally and not remotely. Secondly, when i execute command manually on remote machine, i see the status is saved in file but when doing via script the file is still empty. any hint?

ssh -o StrictHostKeyChecking=no -i test-ssh-key -o 'ProxyCommand ssh -i test-ssh-key -W %h:%p [email protected]' lab@$free_node "echo Test!!123 | sudo -S echo; sudo systemctl start cachefilesd; sudo rm /var/log/cachefiles_status.log; sudo ps x | grep cachefilesd | sudo awk '{print $1}' | sudo sed -n -e 1,1p | xargs -l1 -- sh -c 'sudo  grep "State:" /proc/$1/status' -- | awk -F '(' '{print $2}' | awk -F ')' '{print $1}' > /tmp/cachefiles_status.log; sudo cp /tmp/cachefiles_status.log /var/log/; sudo cat /var/log/cachefiles_status.log; ls -ltr /var/log"

1 Answer 1


Use ssh_config (~/.ssh/config) to set your proxy setting. It will shorten your oneliners significantly.

Same way you pass your commands to the server you can store their output to the temp file, then rsync output files into your local machine, through the SSH proxy. Rsync uses ssh_config when it's present, but you can also specify custom SSH settings via rsync's -e flag.

So your ssh config will be something like:

Host free_node
    StrictHostKeyChecking no
    IdentityFile test-ssh-key
    User lab
    ProxyCommand ssh -i test-ssh-key -W %h:%p [email protected]


rsync -av free_node:/root/output.log ~/

Alternatively if rsync isn't present on your server and you cannot install it, you can use scp instead.

scp free_node:/root/output.log ~/

However I recommend using ansible or any other automation tool for this job. Ansible uses ssh and ad-hoc commands. Python is its only requirement on server side, however with ansible's raw module you don't even need to have python installed on your server. You can also use custom ssh commands to pass your connections through proxy, as describe in ansible faq.

You must log in to answer this question.

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