0

During my work I need to set up many tunnels. What is the best practice to manage it? Sometime tunnels can lost connection I want to auto reconnect it, maybe there is good thing to do it automatically

3
  • You could try to write a bash script that does what you want, I don't work with tunnels so I cant help you sadly, but take a look around! :)
    – patrick
    Aug 2, 2016 at 7:36
  • I take it its a SSH tunnel? or VPN?
    – patrick
    Aug 2, 2016 at 7:37
  • Just simple ssh -D 7070 [email protected]
    – mystdeim
    Aug 2, 2016 at 7:39

1 Answer 1

0

I found this on Stack exchange! give it a shot!

#!/bin/sh

REMOTEUSER=username
REMOTEHOST=remotehost 

SSH_REMOTEPORT=22 
SSH_LOCALPORT=10022

TUNNEL_REMOTEPORT=8080
TUNNEL_LOCALPORT=8080

createTunnel() {
    /usr/bin/ssh -f -N  -L$SSH_LOCALPORT:$REMOTEHOST:SSH_REMOTEPORT -L$TUNNEL_LOCALPORT:$REMOTEHOST:TUNNEL_REMOTEPORT $REMOTEUSER@$REMOTEHOST
    if [[ $? -eq 0 ]]; then
        echo Tunnel to $REMOTEHOST created successfully
    else
        echo An error occurred creating a tunnel to $REMOTEHOST RC was $?
    fi
}

## Run the 'ls' command remotely.  If it returns non-zero, then create a new connection
/usr/bin/ssh -p $SSH_LOCALPORT $REMOTEUSER@localhost ls >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
    echo Creating new tunnel connection
    createTunnel
fi

This script will open two ports, ports 22 which will be used to check if the tunnel is still alive and port 8080 which is the port you might want to use (you can change that ofcourse)

1
  • do note that this is for one ssh, you can probably copy and paste it a few times for different tunnels
    – patrick
    Aug 2, 2016 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .