aboutsummaryrefslogtreecommitdiff
path: root/psshd
blob: 6799ec0d51c4ee0940b9a3819d6e0721df5fcb49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash

#
# Wrapper around autossh and start-stop-daemon for basic persistence. Intended
# to be called as a script from profile.d for automatic SSH tunnels.
#
# Uses one argument, the autossh monitoring port number to use. The remainder
# of the arguments are passed to the autossh binary.
#
#     $ psshd 9001 -fN -D 8001 remotehost
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# License: MIT
#

# Name self
self=psshd

# Extend path to find autossh and start-stop-daemon binaries
PATH=/usr/lib/autossh/autossh:/sbin:$PATH

# Check we have access to the required binaries
cmds=(autossh start-stop-daemon)
for cmd in "${cmds[@]}"; do
    if ! hash "$cmd" >/dev/null; then
        printf '%s - Could not find %s in PATH\n' "$self" "$cmd" >&2
        exit 127
    fi
done

# If no port, give up with usage instructions
if (($# > 0)); then
    port=$1
    shift
else
    printf 'USAGE: %s <port number> -- <ssh arguments>\n' "$self"
    exit 1
fi

# Set up a PID dir
rundir=${TMPDIR:-/tmp}/psshd-$UID
if ! mkdir -p "$rundir"; then
    printf '%s - Could not create directory %s for PID file\n' "$self" "$rundir" >&2
    exit 1
fi

# Export the two settings autossh absolutely needs
AUTOSSH_PIDFILE=$rundir/psshd-port-${port}.pid
AUTOSSH_PORT=$port
export AUTOSSH_PIDFILE AUTOSSH_PORT

# Use start-stop-daemon to run it sanely
start-stop-daemon \
    --start \
    --quiet \
    --pidfile "$AUTOSSH_PIDFILE" \
    --exec "$(type -P autossh)" \
    -- "$@"