Witam na mojej prywatnej stronie internetowej!
[If this is all Polish to you, click here: English]
Uwaga: z oczywistych powodów nie mogę zagwarantować swojej nieomylności, choć staram się o zgodność tego, co piszę, z Prawdą. Jest również oczywiste, że nie gwarantuję takiej zgodności w przypadku komentarzy. Umieszczenie linku do strony spoza niniejszego serwisu nie musi oznaczać, że podzielam poglądy autora tej strony, a jedynie, że uważam ją za wartościową z takich czy innych powodów.
Marcin ‘mbork’ Borkowski
I have a script which performs several operations on some remote server, issuing a number of ssh
commands, more or less like this:
scp project.zip scp://some_server/~ ssh ssh://some_server rm -rf ~/project-old ssh ssh://some_server cp -r ~/project ~/project-old ssh ssh://some_server unzip -o ~/project.zip -d ~/project
This obviously works, but is not very optimized. One obvious bottleneck is that every ssh
command needs to create a connection to the remote server, which takes some time. What if it were possible to open the connection once and then reuse it whenever the need arises?
Well, it turns out that not only is this possible, but much easier than I thought. In fact, in the simplest case you don’t even have to change the script at all! Just add something like this to the ~/.ssh/config
file:
Host some_server HostName ... User ... IdentityFile ... ControlMaster auto ControlPath ~/.ssh/%r@%h:%p ControlPersist 1m
This way, the first ssh
command will create a connection and each subsequent command will reuse it. The connection will close automatically after 60 seconds of inactivity. How cool is that? (In the case of my script, I was able to gain a speedup of about 50%!)
I am not 100% sure about the security implications of this solution, though – I don’t see any obvious security holes, but I’m not an expert on that. One thing that might look dangerous is the fact that the connection remains open for one minute after the script is done. If you want to explicitly close it instead of waiting, just say ssh -O
exit some_server
.