Welcome to WebmasterWorld Guest from

Forum Moderators: bakedjake

Message Too Old, No Replies

Monitoring a SSH tunnel

How to determine if session is still active?



11:28 pm on Dec 19, 2007 (gmt 0)

5+ Year Member

If I set up a SSH tunnel using remote port forwarding from one Linux machine to another, how can I check to see if the tunnel is still up?

I realize there are tools like RSTunnel and autossh, but I'm wondering how one goes about determining, via a script, if a session/process is still running.




11:30 pm on Jan 15, 2008 (gmt 0)

5+ Year Member

Anyone? I'm guessing a combination of bash and cron scripts, though I'm not sure what the best way to test if a SSH tunnel is still active.


12:08 am on Jan 16, 2008 (gmt 0)

WebmasterWorld Senior Member lammert is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

On linux, the command netstat -nt grep yourip:22 should give you a list of currently active ssh connections. That list can be further processed in a script.


8:58 pm on Jan 16, 2008 (gmt 0)

5+ Year Member

Thanks lammert. I realized I can do something similar with ps and grep. I've created a bash script that checks if the SSH session is still up and reconnects if not.

I have a feeling that there is more to this, but I've definitely made some progress.

Here is what I have thus far (comments/suggestions welcome):


while true
if ps aux grep [y]ourdomain
echo "SSH connection up."
echo "SSH connection down."
echo "Trying to connect ..."
ssh -N -f -R 3307:localhost:3306 user@www.yourdomain.com
sleep 60


Featured Threads

Hot Threads This Week

Hot Threads This Month