Results 1 to 2 of 2

Thread: create ssh tunnel only if it doesn't exist on the port yet

  1. #1
    Join Date
    Jul 2014
    Beans
    340

    Question create ssh tunnel only if it doesn't exist on the port yet

    Hi all,

    I'm looking for a way to create ssh tunnel only in case it doesn't exist on particular port yet.

    This is how I would check if the tunnel exists:

    Code:
    nc -z 127.0.0.1 9521 || echo "no tunnel open"
    And this is how I would create the tunnel:

    Code:
    ssh user1@example.com -D9521 -C -N -q
    Now how do I combine those two or maybe there is another more robust way?... The thing is that I'm not able to install additional software on the system; also I'm not able to copy some pre-compiled executable into the filesystem -- the instance is re-created from scratch every session. I would rather need just to combine those two commands above using bash.

    Please advise.

  2. #2
    Join Date
    Mar 2010
    Location
    Been there, meh.
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: create ssh tunnel only if it doesn't exist on the port yet

    Code:
    # Only start SOCKS proxy if necessary
    
    if  [ $(/bin/ps -eaf |/bin/grep ssh |/bin/grep -c $PORT ) = 0 ] ; then
       # Setup SOCKS proxy through home server
       echo "Starting ssh SOCKS Proxy"
       /usr/bin/ssh  -f -C -D $PORT  $SSH_SRV  -NT &
    fi
    is how I do it.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •