Determining the port assigned by -R 0

Damien Miller djm at
Sat Oct 15 10:13:57 EST 2011

On Fri, 14 Oct 2011, Glenn Maynard wrote:

> It's difficult to programmatically determine the port dynamically assigned
> by -R 0.  The port is output in plain (and presumably localized) text, but
> I've found that by far the most common use of this is for scripts.
> Is there currently any sane way to get this information?  For the short term
> I'll probably just parse the plaintext "Allocated port..." message, but
> that's obviously not a reliable solution.  It would be helpful if a list of
> forwarded ports was stored in a parsable environment variable, as with

It's easier if you use the multiplexing socket. 

ssh -O forward -R0:xxx:yyy host

will print the allocated port to stdout on success.

More information about the openssh-unix-dev mailing list