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?
Use autossh. It's the tool that's meant for monitoring the ssh connection.
These are more detailed steps to test or troubleshoot an SSH tunnel. You can use some of them in a script. I'm adding this answer because I had to troubleshoot the link between two applications after they stopped working. Just grepping for the ssh process wasn't enough, as it was still there. And I couldn't use
nc -z
because that option wasn't available on my incantation of netcat.Let's start from the beginning. Assume there is a machine, which will be called local with IP address 10.0.0.1 and another, called remote, at 10.0.3.12. I will prepend these hostnames, to the commands below, so it's obvious where they're being executed.
The goal is to create a tunnel that will forward TCP traffic from the loopback address on the remote machine on port 123 to the local machine on port 456. This can be done with the following command, on the local machine:
To check that the process is running, we can do:
If you see the command in the output, we can proceed. Otherwise, check that the SSH key is installed in the remote. Note that excluding the username before the remote IP, makes ssh use the current username.
Next, we want to check that the tunnel is open on the remote:
We should get an output similar to this:
Would be nice to actually see some data going through from the remote to the host. This is where netcat comes in. On CentOS it can be installed with
yum install nc
.First, open a listening port on the local machine:
Then make a connection on the remote:
If you open a second terminal to the local machine, you can see the connection. Something like this:
Better still, go ahead and type something on the remote:
You should see this being mirrored on the local terminal:
The tunnel is working! But what if you have an application, called appname, which is supposed to be listening on port 456 on the local machine? Terminate nc on both sides then run your application. You can check that it's listening on the correct port with this:
By the way, running the same command on the remote should show sshd listening on port 127.0.0.1:123.
stunnel is a good tool to make semi-permanent connections between hosts.
http://www.stunnel.org/
If you are using ssh in background, use this:
This is really more of a serverfault-type question, but you can use netstat.
something like:
This will tell you if there's an ssh process listening on the specified port. it will also tell you the PID of the process.
If you really want to double-check that the ssh process was started with the right options, you can then look up the process by PID in something like
We can check using ps command
Will show all shh service running and we can find the tunnel service listed