blob: b669e1dfd3b4901aeb044b901ce56a97683c9b21 (
plain)
- #!/bin/sh
- # Wrapper around autossh to always keep open ssh and web tunnels
- # Usable as cronjob and ifup hook
- set -e
- DEFAULTS=/etc/local/autoautossh
- DAEMON=/usr/bin/autossh
- # Set defaults
- DEFAULTLOCALUSER=autossh
- DEFAULTLOCALIFACE=eth0
- DEFAULTREMOTEHOST=defaultserver
- DEFAULTREMOTESSHPORT=2200
- DEFAULTREMOTEHTTPPORT=2280
- # Check for daemon presence
- [ -x "$DAEMON" ] || exit 0
- # Include config defaults if available
- [ -r "$DEFAULTS" ] && . "$DEFAULTS"
- # allow overriding these in environment
- LOCALUSER=${LOCALUSER:-$DEFAULTLOCALUSER}
- LOCALIFACE=${LOCALIFACE:-$DEFAULTLOCALIFACE}
- REMOTEHOST=${REMOTEHOST:-$DEFAULTREMOTEHOST}
- REMOTESSHPORT=${REMOTESSHPORT:-$DEFAULTREMOTESSHPORT}
- REMOTEHTTPPORT=${REMOTEHTTPPORT:-$DEFAULTREMOTEHTTPPORT}
- # ifup hack: Ignore ifup'ing irrelevant interfaces (including loopback)
- # Unset LOCALIFACE to always continue
- [ -z "$LOCALIFACE" ] || [ -z "$IFACE" ] || [ "$IFACE" = eth0 ] || exit 0
- # Back out if possible to reach ourselves from outside
- ! su "$LOCALUSER" -c "ssh '$REMOTEHOST' -- ssh -C -o ConnectTimeout=5 -o ConnectionAttempts=3 -p '$REMOTESSHPORT' localhost -- true" || exit 0
- # SSH tunnel is broken - establish fresh tunnels
- killall autossh || true
- 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'" &
- exit 0
|