Skip to content

Instantly share code, notes, and snippets.

@millsoft
Created September 28, 2019 14:46
Show Gist options
  • Save millsoft/9a5f8f0bec65927812700ad4cdfcd6d8 to your computer and use it in GitHub Desktop.
Save millsoft/9a5f8f0bec65927812700ad4cdfcd6d8 to your computer and use it in GitHub Desktop.
SSH Tunnel with auto reconnect
#!/bin/sh
# endless ssh tunnel. Makes sure to reconnect if the connection breaks (eg "broken pipe")
# why? if you want to expose your localhost to the public without dealing with firewalls / routers - works similar to ngrok and is free
#
# what do I need?
# you will need an external server with ssh access.
#
# how?
# you will need to create a ssh key for your external server so you don't need a password. (important for reconnecting)
# Edit the ports and ssh command, make this script executable and execute it
# This script simply runs ssh command in an endless loop. So if the connection breaks, it will reconnect asap.
# To stop the tunnel press CTRL+C
remote_port=8080
local_port=80
[email protected]
echo "***************************************"
echo "*** SSH TUNNEL"
echo "*** LOCAL PORT = $local_port REMOTE PORT = $remote_port"
echo "***************************************"
while :
do
CURRENT_TIME=$(date +"%Y-%m-%d %H:%M:%S")
echo "$CURRENT_TIME connecting"
ssh -nN -R ${remote_port}:localhost:${local_port} $ssh 2> /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment