ssh: check if a tunnel is alive

Adam Matan picture Adam Matan · Jan 4, 2010 · Viewed 106.5k times · Source

I have written a small bash script which needs an ssh tunnel to draw data from a remote server, so it prompts the user:

echo "Please open an ssh tunnel using 'ssh -L 6000:localhost:5432 example.com'"

I would like to check whether the user had opened this tunnel, and exit with an error message if no tunnel exist. Is there any way to query the ssh tunnel, i.e. check if the local port 6000 is really tunneled to that server?

Answer

rxw picture rxw · Jun 22, 2014

Netcat is your friend:

nc -z localhost 6000 || echo "no tunnel open"