How to know when the listening socket is ready
R. Diez
rdiez-2006 at rd10.de
Mon Mar 30 00:50:29 AEDT 2026
Hi all:
I am using 'ssh' to set up a tunnel on demand. Conceptually, it looks like this:
ssh <create the tunnel>
use-the-tunnel.sh
One problem I have with 'ssh' is knowing when the tunnel is ready. If use-the-tunnel.sh tries to connect immediately, it will probably fail, as 'ssh' needs some time. How long it needs depends on the weather.
I have encountered this problem somewhere else, the last time around with OpenOCD's listening sockets. Somehow, network software tends to expect that other tools magically know when their listening sockets are ready.
I even had to write a generic tool for that purpose:
https://github.com/rdiez/Tools/tree/master/WaitForTcpPort
My tool does work, but is slow, inefficient and inconvenient.
Most Linux distributions have to patch OpenSSD to signal systemd when the daemon is ready. More information is here:
"So the distro maintainers patched sshd to use the sd_notify() function from libsystemd.so"
https://news.ycombinator.com/item?id=39944518
Debian changes OpenSSH packaging
https://lwn.net/Articles/991088/
I wonder why very popular Linux distributions have to patch OpenSSH for years, instead of getting the patch upstream. But that is another discussion.
That limited patching would not help me anyway, because I am using the ssh "client" as a service/daemon, and only the "main" daemon (sshd) gets patched.
Is there any other way in which 'ssh' can signal that its listening socket is ready?
In the case of OpenOCD, I can start an external script when its listening sockets are ready. It took me hours to get command escaping through Tcl etc. right, but it works.
However, ssh does not have that capability, right? I mean 'ssh' can start a command on the remote host when the tunnel has been established, but I need the command running on the local computer creating and using the tunnel.
Thanks in advance,
rdiez
More information about the openssh-unix-dev
mailing list