1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/bin/bash tunnel_entrance_port=13306 tunnel_end=username@re.mo.te.ip destination=127.0.0.1 destination_port=3306 # Use netcat to connect to tunnel entrance port. If its exit code is not zero, the tunnel needs to be brought up nc -w1 localhost $tunnel_entrance_port > /dev/null if [[ $? - ne 0 ]]; then echo "creating new tunnel for MySQL" /usr/bin/ssh -f -N -L $tunnel_entrance_port:$destination:$destination_port $tunnel_end if [[ $? - eq 0 ]]; then echo " tunnel to $destination through $tunnel_end created successfully" else echo " an error occurred creating a tunnel to $destination through $tunnel_end" fi fi |