~acdw/mrgrctrnl

ref: 386e6c03cc8bcd618c53f9a47021ebba1c64b2f3 mrgrctrnl/mrgrctrnl -rwxr-xr-x 3.7 KiB
386e6c03Case Duckworth Version bump 1 year, 2 months ago
                                                                                
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/sh
# mrgrctrnl: configurable ssh tunneler
# Author: Case Duckworth <acdw@acdw.net>
# Version: 0.3.1
# License: MIT

PRGN="${0##*/}"

# entry point
main() {
	# flags
	_RUN=true
	_LOG=true
	_USE_CONFIG=true
	_RESTART=false
	# options
	__CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/$PRGN/config"
	__PIDF="/tmp/$PRGN.pid"
	__CMDF="/tmp/$PRGN.cmd"
	__SSH="$(command -v autossh || command -v ssh)"
	__SSH_ARGS="-nT -N \"\$user@\$machine\" -L \"\$local:\$remote\""

	# parse options
	while getopts hkrnqSAc:s: OPT; do
		case "$OPT" in
		h)
			usage
			exit 0
			;;
		k)
			untunnel
			pkill -x "$PRGN"
			exit "$?"
			;;
		r) _RESTART=true ;;
		n) _RUN=false ;;
		q) _LOG=false ;;
		S) __SSH='ssh' ;;
		A) __SSH='autossh' ;;
		c) __CONFIG="$OPTARG" ;;
		s)
			__SSH_ARGS="$OPTARG"
			_USE_CONFIG=false
			;;
		*) exit 4 ;;
		esac
	done
	shift $((OPTIND - 1))

	# save command invocation (for -r)
	if $_RESTART; then
		untunnel
		exec "$PRGN" $(cat "$__CMDF")
	else
		printf '%s\n' "$*" >"$__CMDF"
	fi

	# make sure ssh is installed
	if ! command -v "$__SSH" >/dev/null; then
		log "Not installed: $__SSH"
		exit 2
	fi

	# make sure the config is readable
	if $_USE_CONFIG && [ ! -r "$__CONFIG" ]; then
		log "Cannot find config file $__CONFIG; aborting."
		exit 3
	fi

	# load the config
	if $_USE_CONFIG; then
		awk '{sub(/#.*$/,"");print}' "$__CONFIG" |
			while read -r machine user local remote key rest; do
				if [ -z "$machine" ] ||
					[ -z "$user" ] ||
					[ -z "$local" ] ||
					[ -z "$remote" ]; then
					continue
				fi

				case "$local" in
				:*) local="localhost$local" ;;
				esac
				case "$remote" in
				:*) remote="localhost$remote" ;;
				esac

				# shellcheck disable=2030
				if [ -n "$key" ]; then
					__SSH_ARGS="$__SSH_ARGS -i \"$key\""
				fi
				if [ -n "$rest" ]; then
					__SSH_ARGS="$__SSH_ARGS $rest"
				fi

				eval "tunnel_${__SSH##*/} $__SSH_ARGS" &
			done
	else
		#shellcheck disable=2031
		eval "tunnel_${__SSH##*/} $__SSH_ARGS" &
	fi

	wait
}

usage() {
	cat <<END
$PRGN: make magic ssh tunnels
usage:	$PRGN [-h] [-k] [-r]
	$PRGN [-n] [-q] [-S|-A] [-c FILE | -s ARGS]

options:
	-h	show this help
	-k	demolish all tunnels and exit
	-r	demolish all tunnels and restart
	-n	dry run: just print what would happen
	-q	quiet: don't print anything

	-S	force use of 'ssh'
	-A	force use of 'autossh'
		default: use 'autossh' if installed, otherwise 'ssh'

	-c FILE	load FILE as config file
		default:	\$XDG_CONFIG_HOME/$PRGN/config

	-s ARGS	run CMD with ARGS, do not read config file
		default:	'-N USER@MACHINE -L LOCAL:REMOTE'
				(populated from config file)

config example:
	# a hash mark comments to the end of the line
	# if either 'local' or 'remote' begin with a colon,
	# they'll be prefixed with 'localhost'
	# machine	user	local	remote	key	other
	example.com	bill	:1299	:6667	# key and other are optional
END
}

tunnel_ssh() {
	set -- ssh "$@"
	log "$@"
	$_RUN || return

	touch "$__PIDF"
	while [ -e "$__PIDF" ]; do # quit if the pid file is removed
		# make sure we haven't tunneled this connection already
		if grep -q -e "$*" "$__PIDF"; then
			sleep 3
			continue
		fi

		# try to start ssh tunnel
		if "$@" & then
			printf '%s\t%s\n' "$!" "$*" >>"$__PIDF"
		else
			badpid="$!"
			kill "$badpid"
			sed "/^$badpid/d" "$__PIDF" >"$__PIDF~" &&
				mv "$__PIDF~" "$__PIDF"
			continue
		fi
		sleep 3
	done
}

tunnel_autossh() {
	set -- autossh -f "$@"
	log "$@"
	$_RUN || return

	"$@" && printf '%s\t%s\n' "$!" "$*" >"$__PIDF"
}

untunnel() {
	log "Killing tunnels"
	if [ -e "$__PIDF" ]; then
		while read -r pid _; do
			kill "$pid" 2>/dev/null
		done <"$__PIDF"
		rm "$__PIDF"
	else
		pkill -x ssh
		pkill -x autossh
	fi
	log "Done"
}

log() {
	$_LOG && printf '%s: %s\n' "$PRGN" "$*" >&2
}

main "$@"