Dima
Dima

Reputation: 1828

Not closing ssh

I have a korn 88 shell script which creates a folder on the remote host using the following command:

ssh $user@$host "mkdir -p $somedir" 2>> $Log

and after that transfers a bunch of files in a loop using this

scp -o keepalive=yes $somedir/$file $user@$host:$somedir

I wonder if first command will leave connection open after script ends?

Upvotes: 2

Views: 1068

Answers (2)

evil otto
evil otto

Reputation: 10582

New-enough versions of ssh have the ability to multiplex several virtual connections over a single physical connection. So what you could do is start up some long-running ssh command in the background with connection multiplexing enabled, and then subsequent connections will re-use that connection with much faster startup times. See the manpage for ssh_config for info on connection multiplexing, relevant options are ControlMaster and ControlPath.

But as William Pursell suggests, rsync is probably easier and faster, if it's an option.

Upvotes: 3

Adam Zalcman
Adam Zalcman

Reputation: 27233

Each of the commands opens and closes its own connection. It's easy to use a tool like tcpdump to verify this.

This is a consequence of the fact that the exit() system call used to terminate a process closes all open file descriptors including socket file descriptors. Closing a socket closes the connection behind the socket.

Upvotes: 3

Related Questions