Skip to content

Instantly share code, notes, and snippets.

@poige
Created July 17, 2015 12:04
Show Gist options
  • Save poige/a87761fff67d4960741e to your computer and use it in GitHub Desktop.
Save poige/a87761fff67d4960741e to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Tries to keep SSH-forwarding connection open for port forwarding
# using given range of local ports.
#
UserHost='SPECIFY_USER@HOST.HERE'
PortMin=20200
PortMax=20222
AliveChkSec=17
AliveChkCnt=3
Port="$PortMin"
while :; do
ssh \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-o ServerAliveInterval="$AliveChkSec" \
-o ServerAliveCountMax="$AliveChkCnt" \
-o ExitOnForwardFailure=yes \
-C -R"$Port":127.0.0.1:22 \
"$UserHost"
Port=$(($Port+1))
if [ "$Port" -gt "$PortMax" ]; then
Port="$PortMin"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment