Answer the question
In order to leave comments, you need to log in
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
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
if I'm not mistaken, when you specify port "0" sshd chooses a random port and informs the client.
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question