I am trying to find the simplest way to upload a file using ssh and after that run a command on the remote machine within the same ssh session for some post-processing, so that I don't need to login again. The upload should, if possible, show some progress indicator.

So far I looked into scp and rsync, and both are not capable of running any hooks. (I could use the --rsync-path parameter to execute some script before rsync) but I want to do post-processing. Is there any way to open a ssh session, upload, execute a command and close it again?


You might want the ControlMaster mechanism in ssh.

  • Can this be done without the config file in .ssh? – janoliver Apr 15 '12 at 9:01
  • 1
    You could use the equivalent -o options, but you really want the config file given the number of options you need to specify and the importance of their being consistent across all the invocations. – geekosaur Apr 15 '12 at 9:04
  • Using controlmaster I found the answer here: serverfault.com/questions/78630/… Thank you for the help! – janoliver Apr 15 '12 at 9:21

Well, you could just pipe the whole thing through one ssh command with a cat on the other end. Here I upload a shell script, make it executable, and run it:

$ cat sayhi.sh | ssh myserver 'cat > ./remotehi.sh ; chmod +x ./remotehi.sh ; ./remotehi.sh'
hello, world!
  • 1
    To get the progress you can replace cat with pv. so pv sayhi.sh | ssh myserver 'cat > remotehi.sh.... @ckhan the ./ isnt necessary on your cat or chmod commands. Wont hurt, but not necessary. – Patrick Apr 15 '12 at 15:04
  • That is a nice solution as well. Thank you both! – janoliver Apr 16 '12 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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