aboutsummaryrefslogtreecommitdiff
path: root/psshd
blob: cbbb1bacc4c17eac130a5f4966c357dda0a068a0 (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
59
60
61
62
63
64
65
66
67
#!/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.
#
# Takes one required option -p, the autossh monitoring port number to use. Cut
# the options off with -- and the remainder of the arguments are passed to the
# autossh binary.
#
# $ psshd -p 9001 -- -fN -D 8001 remotehost
#
# @author Tom Ryder <tom@sanctum.geek.nz>
# @copyright 2013
#

# Just stop if any problems
set -o errexit

# Path to autossh binary (not any wrapper script)
autossh=/usr/lib/autossh/autossh

# Path to start-stop-daemon binary
startstopdaemon=/sbin/start-stop-daemon

# Set up a PID dir
dir=${TMPDIR:-/tmp}/psshd-${UID}
mkdir -p $dir

# Get port in options
while getopts ':p:' opt
do
    case $opt in
        p)
            port=$OPTARG
            ;;
        \?)
            echo "Invalid option $OPTARG" >&2
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires an argument" >&2
            exit 1
            ;;
    esac
done
shift $((OPTIND-1))

# If no port, give up with usage instructions
if [[ ! -n "$port" ]]
then
    echo "USAGE: $0 -p <port number> -- <ssh arguments>"
    exit 1
fi

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

# Use start-stop-daemon to run it sanely
$startstopdaemon \
    --start \
    --quiet \
    --pidfile $AUTOSSH_PIDFILE \
    --exec $autossh \
    -- "$@"