How to confirm the port is available before connecting with reverse SSH
To set a reverse SSH connection I specify the ssh -R
command line flag followed by a remote machine's port 2210
to map it to a localhost:22
of the machine requesting a reverse ssh connection:
ssh -R 2210:localhost:22 [email protected]
Is there a way to check if the remote port 2210
is already in use and raise an error if if the port is not available?
ExitOnForwardFailure
Specifies whetherssh(1)
should terminate the connection if it cannot set up all requested dynamic, tunnel, local, and remote port forwardings, (e.g. if either end is unable to bind and listen on a specified port). […] The argument must beyes
orno
(the default).
(source: man 5 ssh_config
)
Specify it as a command-line option:
ssh -o ExitOnForwardFailure=yes -R …
or in ~/.ssh/config
, or in /etc/ssh/ssh_config
.