mktar: Use `wc` instead of `du` in summary message
[sunny256-utils.git] / ssht
blobcc9a3a0dae3d473dda07c63d2e6dbf29669a0cdf
1 #!/usr/bin/env bash
3 #=======================================================================
4 # ssht
5 # File ID: 307e8876-e099-11e0-94c2-ef2688b28a12
7 # Create/terminate SSH tunnel or set up VNC connection.
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
13 progname=ssht
14 VERSION=0.2.2
16 ARGS="$(getopt -o "\
23 V:\
25 " -l "\
26 connect,\
27 help,\
28 kill,\
29 list,\
30 quiet,\
31 tunnel,\
32 vnc:,\
33 verbose,\
34 version,\
35 " -n "$progname" -- "$@")"
36 test "$?" = "0" || exit 1
37 eval set -- "$ARGS"
39 opt_connect=0
40 opt_help=0
41 opt_kill=0
42 opt_list=0
43 opt_quiet=0
44 opt_tunnel=0
45 opt_vnc=''
46 opt_verbose=0
47 while :; do
48 case "$1" in
49 -c|--connect) opt_connect=1; shift ;;
50 -h|--help) opt_help=1; shift ;;
51 -k|--kill) opt_kill=1; shift ;;
52 -l|--list) opt_list=1; shift ;;
53 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
54 -t|--tunnel) opt_tunnel=1; shift ;;
55 -V|--vnc) opt_vnc=$2; shift 2 ;;
56 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
57 --version) echo $progname $VERSION; exit 0 ;;
58 --) shift; break ;;
59 *) echo $progname: Internal error >&2; exit 1 ;;
60 esac
61 done
62 opt_verbose=$(($opt_verbose - $opt_quiet))
64 rcfile="$HOME/.sshtrc"
66 if test "$opt_help" = "1"; then
67 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
68 cat <<END
70 Create/terminate SSH tunnel or set up VNC connection.
72 Usage: $progname [options]
74 Options:
76 -c, --connect
77 Connect to SSH tunnel from outside.
78 -h, --help
79 Show this help.
80 -k, --kill
81 Kill SSH tunnel.
82 -l, --list
83 List PID of active tunnel.
84 -q, --quiet
85 Be more quiet. Can be repeated to increase silence.
86 -t, --tunnel
87 Create SSH tunnel if none exist yet.
88 -v, --verbose
89 Increase level of verbosity. Can be repeated.
90 -V DISPLAY [QUALITY], --vnc DISPLAY [QUALITY]
91 Connect via VNC to display DISPLAY. QUALITY can be 0 (worst, but
92 great compression) to 9 (best quality). If QUALITY is not specified,
93 top quality and no compression is used.
94 --version
95 Print version information.
97 The file "$rcfile"
98 is a regular shell script used for setting constants:
100 SSHT_PORT
101 Port number for SSH tunnel. Choose something random above 1024,
102 preferably not listed in /etc/services .
103 SSHT_HOST
104 SSH username and host on the format USER@HOST .
105 SSHT_USERNAME
106 SSH username used with -c/--connect option.
107 SSHT_CMD
108 Command to set up the SSH tunnel. If not defined, a default value is
109 used.
112 exit 0
115 msg() {
116 echo "$progname: $*" >&2
119 test -e "$rcfile" && . "$rcfile"
121 if test -z "$SSHT_PORT"; then
122 msg SSHT_PORT environment variable is not defined
123 exit 1
125 if test -z "$SSHT_HOST"; then
126 msg SSHT_HOST environment variable is not defined
127 exit 1
129 if test -z "$SSHT_CMD"; then
130 SSHT_CMD="ssh -A -Y -q -N -R $SSHT_PORT:localhost:22 $SSHT_HOST"
133 unset SSHT_PID
134 SSHT_PID=$(pgrep -f -x "$SSHT_CMD")
136 if test "$opt_connect" = "1"; then
137 if test -z "$SSHT_USERNAME"; then
138 msg SSHT_USERNAME environment variable is not defined
139 exit 1
141 sess -t c_ssht.c -- ssh -A -Y -p $SSHT_PORT -l $SSHT_USERNAME localhost
142 elif test "$opt_kill" = "1"; then
143 test -n "$SSHT_PID" || { msg No tunnel found; exit 1; }
144 kill $SSHT_PID
145 elif test "$opt_list" = "1"; then
146 test -n "$SSHT_PID" && echo $SSHT_PID
147 elif test "$opt_tunnel" = "1"; then
148 test -n "$SSHT_PID" && {
149 msg Tunnel already active at PID $SSHT_PID
150 exit 1
152 # FIXME: Create some standardised version of this
153 ps x | grep "bash -c while :; do date; sleep 2; done" |
154 grep -v "grep " | awk '{print $1}' | xargs -r kill
155 sess -t c_ssht.t -- $SSHT_CMD &
156 elif test -n "$opt_vnc"; then
157 msg Running pkill...
158 pkill -9 -f "ssh -L 590$opt_vnc:localhost:590$opt_vnc"
159 msg Creating ssh tunnel...
160 sess -d ssh_tunnel -- \
161 ssh -L 590$opt_vnc:localhost:590$opt_vnc -C -N -f $SSHT_HOST
162 quality_str=
163 if test -n "$1"; then
164 quality_str="-quality $1"
165 msg Starting xtightvncviewer with quality $1...
166 sess -- xtightvncviewer -encodings tight -depth 16 -compresslevel 9 \
167 -nocursorshape $quality_str localhost:$opt_vnc
168 else
169 msg Starting xtightvncviewer with top quality...
170 sess -- xtightvncviewer -encodings tight -truecolour -nojpeg \
171 -nocursorshape localhost:$opt_vnc
173 msg Destroying ssh tunnel...
174 pkill -9 -f "ssh -L 590$opt_vnc:localhost:590$opt_vnc"
175 else
176 msg No option specified
177 exit 1