blob: e270ea153b66cd8d5c7004507249e7b48c8fc8fb (
plain)
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
|
#!/bin/sh
if [ "$1" = "--help" ]; then
echo "Usage: $0 [-p] [<pid-file>]" 1>&2
echo " -p: Print only pid of SSH process (if connecting or connected)." 1>&2
echo " Otherwise, print status" 1>&2
echo " Exit code: 0 if connected, 1 if connecting, 2 if not connected." 1>&2
echo " $0 -s [<pid-file>]" 1>&2
echo " -s: Stop the tunnel" 1>&2
exit 255
fi
if [ "$1" = "-p" ]; then
showpid=1
shift
elif [ "$1" = "-s" ]; then
stop=1
shift
fi
pidfile="$1"
[ -z "$pidfile" ] && pidfile="$HOME/pid"
connected()
{
[ "$showpid" = 1 ] && echo $pid || echo "Connected"
exit 0
}
connecting()
{
[ "$showpid" = 1 ] && echo $pid || echo "Connecting"
exit 1
}
not_connected()
{
[ "$showpid" = 1 ] || echo "Not connected"
exit 2
}
stop_it()
{
kill $pid 2>/dev/null
exit 0
}
pid=`cat "$pidfile" 2>/dev/null`
[ "$pid" ] || not_connected
kill -s 0 $pid 2>/dev/null || not_connected
for i in /proc/$pid/fd/*; do
socket=`readlink $i 2>/dev/null | sed -n 's/^socket:\[\(.*\)\]$/\1/p'`
[ "$socket" ] || continue
state=`awk '$10 == '"$socket"' {print $4}' /proc/net/tcp`
[ "$state" ] || continue
[ "$stop" = 1 ] && stop_it
[ "$state" = "01" ] && connected || connecting
done
not_connected
|