4

I have a shell script that connects via SSH to the same host (my-server) and runs a variety of commands mixed throughout the script. For example:

ssh -o ConnectTimeout=3 -o ConnectionAttempts=3 user@my-server "foocommand here"

mkdir -p /path/here
touch service.conf
...

ssh -o ConnectTimeout=3 -o ConnectionAttempts=3 user@my-server "barcommand here"

mkdir -p /other/path/here
touch /other/path/here/service.conf
...

ssh -o ConnectTimeout=3 -o ConnectionAttempts=3 user@my-server "darcommand here"

And so forth. The problem is each SSH connection open and handshake takes a bit of time as the server my-server is geographically far away from the running script.

Is there a way to speed up this process and prevent opening a new SSH connection for each required command? Anything like http keep alive for SSH?

5

Yes, you can set up ssh to keep a persistent connection to a remote server. This is done with the ControlMaster, ControlPath and ControlPersist options.

A sample configuration (place in $HOME/.ssh/config):

ControlMaster auto
ControlPath ~/.ssh/sockets/%C
ControlPersist 600

Setting ControlPath enables connection sharing; while an ssh connection to a remote host is open other ssh connections to the same user/host will be multiplexed over the connection.

Setting ControlPersist allows the connection to remain active for a period of time after the last ssh session exits.

Setting ControlMaster to auto allows ssh to reuse an existing connection or create a new connection if necessary.

See the ssh_config(5) man page for a detailed explanation of these options.

  • Since the first SSH connection exits immediately after running the inline command foocommand here, would there ever be an existing connection to reuse though? – Justin Jul 6 '18 at 19:40
  • 1
    Yes, of course. That's the whole point of ControlPersist! – Michael Hampton Jul 6 '18 at 19:46

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.