Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
See also SSH easily copy file to local system – Gilles Apr 15 '12 at 22:51
up vote 4 down vote accepted

You might want the ControlMaster mechanism in ssh.

share|improve this answer
Can this be done without the config file in .ssh? – janoliver Apr 15 '12 at 9:01
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!
share|improve this answer
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 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.