I have an ssh connection and I have to send some local files and output to the ssh server.

I know I can do this using the following command

tar zcf - stuff | ssh user@server 'tar zxf -'

I would like to set it up in sequence using the && operator. I already have a connection and I don't want to relogin for every pipe, since I'll be using only one at a time.

Just to clarify I'm not just moving tarballs over the network but also input from devices collected from time to time as the pipeline opens. Just to stop people from saying: "why don't you use this program to move your file over the internet" or "you could also use a usb drive to transfer those files"


You can use persistent connections. In your ~/.ssh/config file, add lines like this.

Host server.name.or.wildcard.goes.here
ControlPath /tmp/ssh.%r@%h:%p
ControlMaster auto
ControlPersist yes
ServerAliveInterval 300
ServerAliveCountMax 15

This will tell ssh to keep the connection open with a file in /tmp that's based on the user/host/port combination. After you connect once, ssh will reuse an existing connection. It will eventually time out and disconnect automatically. This way you only get your password prompt once for several commands running in quick succession, up to a specified amount of time. If the connection is idle for too long, it will be closed and you will need to type a password again.

Keeping the connection open also removes the initial delay of creating a secure connection. This is great for scripts where you need to do some action like copy a file, then wait, then copy another file, etc. cdist uses this technique to speed up machine provisioning.

You can also look into making ssh keys to avoid the password completely.

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.