7

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?

janoliver
  • 1,776

2 Answers2

8

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!
ckhan
  • 4,132
  • 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. – phemmer Apr 15 '12 at 15:04
  • That is a nice solution as well. Thank you both! – janoliver Apr 16 '12 at 07:10
4

You might want the ControlMaster mechanism in ssh.

geekosaur
  • 32,047
  • Can this be done without the config file in .ssh? – janoliver Apr 15 '12 at 09: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 09:04
  • Using controlmaster I found the answer here: http://serverfault.com/questions/78630/retaining-ssh-session-in-bash-script Thank you for the help! – janoliver Apr 15 '12 at 09:21