summaryrefslogtreecommitdiff
path: root/localautoautossh
blob: b669e1dfd3b4901aeb044b901ce56a97683c9b21 (plain)
  1. #!/bin/sh
  2. # Wrapper around autossh to always keep open ssh and web tunnels
  3. # Usable as cronjob and ifup hook
  4. set -e
  5. DEFAULTS=/etc/local/autoautossh
  6. DAEMON=/usr/bin/autossh
  7. # Set defaults
  8. DEFAULTLOCALUSER=autossh
  9. DEFAULTLOCALIFACE=eth0
  10. DEFAULTREMOTEHOST=defaultserver
  11. DEFAULTREMOTESSHPORT=2200
  12. DEFAULTREMOTEHTTPPORT=2280
  13. # Check for daemon presence
  14. [ -x "$DAEMON" ] || exit 0
  15. # Include config defaults if available
  16. [ -r "$DEFAULTS" ] && . "$DEFAULTS"
  17. # allow overriding these in environment
  18. LOCALUSER=${LOCALUSER:-$DEFAULTLOCALUSER}
  19. LOCALIFACE=${LOCALIFACE:-$DEFAULTLOCALIFACE}
  20. REMOTEHOST=${REMOTEHOST:-$DEFAULTREMOTEHOST}
  21. REMOTESSHPORT=${REMOTESSHPORT:-$DEFAULTREMOTESSHPORT}
  22. REMOTEHTTPPORT=${REMOTEHTTPPORT:-$DEFAULTREMOTEHTTPPORT}
  23. # ifup hack: Ignore ifup'ing irrelevant interfaces (including loopback)
  24. # Unset LOCALIFACE to always continue
  25. [ -z "$LOCALIFACE" ] || [ -z "$IFACE" ] || [ "$IFACE" = eth0 ] || exit 0
  26. # Back out if possible to reach ourselves from outside
  27. ! su "$LOCALUSER" -c "ssh '$REMOTEHOST' -- ssh -C -o ConnectTimeout=5 -o ConnectionAttempts=3 -p '$REMOTESSHPORT' localhost -- true" || exit 0
  28. # SSH tunnel is broken - establish fresh tunnels
  29. killall autossh || true
  30. su "$LOCALUSER" -c "autossh -fN -M 0 -o BatchMode=yes -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -R '$REMOTESSHPORT':localhost:22 -R '$REMOTEHTTPPORT':localhost:80 '$REMOTEHOST'" &
  31. exit 0