V
V
Vasily Zhukov2012-09-21 10:35:08
SSH
Vasily Zhukov, 2012-09-21 10:35:08

How to select an idle remote port for "ssh -R"

You need to do remote port forwarding, i.e. open a free remote port forwarding it to the local port. But at the same time, you cannot select a specific remote port, because it may already be busy. The ideal would be to tell ssh to open a free remote port, forward it to a free local port, tell me which ports were selected so that I can bring up the listening local port and let everyone know about the raised remote port. This would establish a remote port forwarding connection to the dynamically selected ports.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alukardd, 2012-09-21
@Alukardd

It seems to me that this cannot be solved without crutches ...
So I have only one option - write a script:
1 - generate a random number
2 - climb ssh and netstat (sockstat in the case of FreeBSD) check if the port is free
3 - analyze the result if the port is free, then remember
4 - we carry out a similar procedure (steps 1-3) for the local port
5 - we perform an ssh connection with port forwarding
6 - we report which ports were used

D
den_admin, 2012-09-23
@den_admin

if I'm not mistaken, when you specify port "0" sshd chooses a random port and informs the client.

V
Vasily Zhukov, 2012-09-24
@vip_delete

ssh -R 0 helps, but as it turns out, not all SSH servers allow connections to the remote port from outside, so the FTP server can't connect to the remote port (and the FTP server doesn't support passive mode). Conclusion: the general task of working with FTP via SSH cannot be solved. In private it is possible. In my case, the STAT command turned out to be enough, which shows a list of files in a folder without creating a data transfer channel, unlike the LIST command, which does the same thing, but tries to create a data transfer channel.

X
xxqs, 2012-09-26
@xxqs

Here I described a scenario where a TCP port is reserved for each client device:
txlab.wordpress.com/2012/03/14/improved-call-home-ssh-scripts/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question