openssh-unix-dev October 2011 archive
Main Archive Page > Month Archives  > openssh-unix-dev archives
openssh-unix-dev: Re: Determining the port assigned by -R 0

Re: Determining the port assigned by -R 0

From: Damien Miller <djm_at_nospam>
Date: Fri Oct 14 2011 - 23:13:57 GMT
To: Glenn Maynard <>

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.
openssh-unix-dev mailing list