These are more detailed steps for testing or troubleshooting an SSH tunnel. Some of them can be used in the script. I am adding this answer because I had to eliminate the connection between the two applications after they stopped working. Just grepping for the ssh process was not enough, as it still was. And I could not use nc -z because this option was not available in my netcat spell.
Let it start from the very beginning. Suppose there is a machine that will be called local with an IP address of 10.0.0.1, and another, called remote , at 10.0.3.12. I will add these hostnames to the commands below, so they are obvious where they run.
The goal is to create a tunnel that will redirect TCP traffic from the return address on the remote computer to port 123 to the local computer on port 456. This can be done using the following command on the local computer:
local:~# ssh -N -R 123:127.0.0.1:456 10.0.3.12
To verify that the process is running, we can:
local:~
If you see a command at the exit, we can continue. Otherwise, check that the SSH key is installed on the remote control. Note that with the exception of the username before the remote IP address, ssh uses the current username.
Next, we want to verify that the tunnel is open on the remote control:
remote:~
We should get the same result:
tcp 0 0 10.0.3.12:ssh 10.0.0.1:45988 ESTABLISHED
It would be nice to actually see some data passing from the remote computer to the host. Here is the netcat. On CentOS, you can install it using yum install nc .
First open the listening port on the local computer:
local:~# nc -l 127.0.0.1:456
Then make the connection on the remote control:
remote:~# nc 127.0.0.1 123
If you open the second terminal on the local computer, you will see a connection. Something like that:
local:~
Better yet, go and type in something on the remote:
remote:~# nc 127.0.0.1 8888 Hallo? anyone there?
You should see this being mirrored on the local terminal:
local:~# nc -l 127.0.0.1:456 Hallo? anyone there?
The tunnel is working! But what if you have an application called appname that is supposed to listen on port 456 on the local computer? End nc on both sides, then run the application. You can verify that it is listening on the correct port with this :
local:~
By the way, running the same command on the remote control should show listening sshd on port 127.0.0.1:123.