Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 3 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
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
add comment

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
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
add comment

Your Answer

 
discard

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.