-----Original Message-----
Behalf Of Joke de Buhr Sent: Tuesday, August 17, 2010 2:03 PM
Subject: Re: Dynamically allocated port on reverse forward
Post by ADFHAUHi,
Post by Joke de Buhrssh -R 0:localhost:22 remote_ssh_server
Allocated port 40454 for remote forward ....
before it drops to the shell.
Is there a way of querying the allocated port on the remote site to
make it usable within scripts? For example to execute a command via
ssh on the origin site in this case.
If you could determine the ancestry of the script process, back to the
sshd driving it and then look up the pid in lsof or netstat output,
you could probably do it.
That or if the script had access to logs and the logging level were
high enough.
echo "shell pid: $$, sshd pid: $PPID"
Unfortunately using lsof -p $PPID (or /proc/$PPID) doesn't work in this
case because the login user doesn't have read permissions to query the
sshd process (not the sshd daemon). Unless lsof is executed as root this
doesn't work.
----
You probably know this, but to dismiss the simplest stuff first: You can
specify a port, rather than relying on dynamic allocation. Just use a
number instead of 0. If you pick under 1024 you'll have to be logging in
as root on the remote side as those numbers are reserved.
Of cause I could use a constant port number. But some of the remote servers
are public servers and sometimes I ran into port collisions with other people
using the server. That's why a dynamically allocated port would be preferable.
It would be nice if ssh would export the allocated port the environment, a
query program maybe or something like that. Any way to query the allocated
port.
FAILED IDEA: A nifty trick for local forwards to different machines is to
/etc/hosts
127.0.0.2 local2
127.0.0.3 local3
Alas, when I tested remote forwards to alternate interfaces on the remote
machine, the resolution failed.
netstat -tl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:55313 *:* LISTEN
tcp 0 0 localhost:42267 *:* LISTEN
netstat -tl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 local3:55313 *:* LISTEN
tcp 0 0 local2:42267 *:* LISTEN
You could, of course, just alias 10.0.0.0/8 ip addresses to a local
interface, but that's probably a bit much work.
As a side note, it seems a major disappointment that there's no escape
sequence to list these. On my Ubuntu 10.4 test machines ~# failed to list
remote forwards.
Also, if you dynamically forward multiple ports, how do you tell which
dynamically assigned remote port maps to each local port?
Maybe ssh could export an environment variable like:
SSH_FORWARDED_PORTS="bind_address:port:host:hostport;bind_address:..."