Skip to content

Instantly share code, notes, and snippets.

@duk3luk3
Created December 23, 2017 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duk3luk3/94aa449a7668cf549ecb79bdc8a52901 to your computer and use it in GitHub Desktop.
Save duk3luk3/94aa449a7668cf549ecb79bdc8a52901 to your computer and use it in GitHub Desktop.
Persistent SSH Tunnel
check host ssh_tunnel with address 127.0.0.1
IF FAILED host 127.0.0.1 port 19999 PROTO SSH
with timeout 20 seconds for 2 cycles
then alert
[Unit]
Description=SSH Reverse tunnel
[Service]
Type=simple
Restart=always
RestartSec=60
ExecStart=/usr/bin/bash -c /path/to/ssh_tunnel.sh
User=USER
Group=systemd-journal
[Install]
WantedBy=network.target
#!/bin/bash
# The SSH endpoint to connect to
ADDR=username@example.com
# A SSH Key for the endpoint
IF=/path/to/key/id_rsa
while true; do
# ssh with Keepalive and -R reverse port forwarding
ssh -i $IF -o "ServerAliveInterval=60" -o "ExitOnForwardFailure=yes" -R 19999:localhost:22 $ADDR -N
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment