diff options
author | Case Duckworth | 2020-04-26 09:15:09 -0500 |
---|---|---|
committer | Case Duckworth | 2020-04-26 09:15:09 -0500 |
commit | ab2b0248c8c58099853203cd1f3f26cc69f78c9e (patch) | |
tree | 2f40d6c2197d68638ca181106454c2899c9fc023 | |
parent | Add key and rest fields and improve ssh calls (diff) | |
download | mrgrctrnl-ab2b0248c8c58099853203cd1f3f26cc69f78c9e.tar.gz mrgrctrnl-ab2b0248c8c58099853203cd1f3f26cc69f78c9e.zip |
Update style
-rwxr-xr-x | mrgrctrnl | 94 |
1 files changed, 46 insertions, 48 deletions
diff --git a/mrgrctrnl b/mrgrctrnl index ca17f55..d51be58 100755 --- a/mrgrctrnl +++ b/mrgrctrnl | |||
@@ -5,40 +5,40 @@ | |||
5 | # version 0.1 | 5 | # version 0.1 |
6 | # LICENSE: MIT | 6 | # LICENSE: MIT |
7 | 7 | ||
8 | usage() | 8 | usage() { |
9 | { | ||
10 | cat <<-END | 9 | cat <<-END |
11 | mrgrctrnl: make magic ssh tunnels | 10 | mrgrctrnl: make magic ssh tunnels |
12 | usage: mrgrctrnl [-h] [-c CONF] [-d] [-k] | 11 | usage: mrgrctrnl [-h] [-c CONF] [-d] [-k] |
13 | 12 | ||
14 | -h show this help | 13 | -h show this help |
15 | -c CONF use config file CONF. | 14 | -c CONF use config file CONF. |
16 | . default: \$XDG_CONFIG_HOME/mrgrctrnl/config | 15 | . default: \$XDG_CONFIG_HOME/mrgrctrnl/config |
17 | -d do a dry run: don't actually tunnel | 16 | -d do a dry run: don't actually tunnel |
18 | -k kill all processes and exit | 17 | -k kill all processes and exit |
19 | 18 | ||
20 | END | 19 | END |
21 | exit "${1:-0}" | 20 | exit "${1:-0}" |
22 | } | 21 | } |
23 | 22 | ||
24 | die() | 23 | die() { |
25 | { | ||
26 | [ "$#" -eq 0 ] && exit 1 | 24 | [ "$#" -eq 0 ] && exit 1 |
27 | case "$1" in | 25 | case "$1" in |
28 | 0-9*) ec="$1"; shift ;; | 26 | 0-9*) |
29 | *) ec=1 ;; | 27 | ec="$1" |
28 | shift | ||
29 | ;; | ||
30 | *) ec=1 ;; | ||
30 | esac | 31 | esac |
31 | printf '!!%s: %s\n' "mrgrctrnl" "$*" | 32 | printf '!!%s: %s\n' "mrgrctrnl" "$*" |
32 | exit "$ec" | 33 | exit "$ec" |
33 | } | 34 | } |
34 | 35 | ||
35 | demolish_tunnels() | 36 | demolish_tunnels() { |
36 | { | ||
37 | printf '%s...' "Demolishing tunnels" | 37 | printf '%s...' "Demolishing tunnels" |
38 | while read -r pid; do | 38 | while read -r pid; do |
39 | [ -z "$pid" ] && continue | 39 | [ -z "$pid" ] && continue |
40 | kill "$pid" 2>/dev/null | 40 | kill "$pid" 2>/dev/null |
41 | done < "$pidf" | 41 | done <"$pidf" |
42 | rm "$pidf" | 42 | rm "$pidf" |
43 | sleep 3 | 43 | sleep 3 |
44 | printf '%s.\n' "Done" | 44 | printf '%s.\n' "Done" |
@@ -50,48 +50,46 @@ dry_run=false | |||
50 | 50 | ||
51 | while getopts hc:dk opt; do | 51 | while getopts hc:dk opt; do |
52 | case "$opt" in | 52 | case "$opt" in |
53 | h) usage ;; | 53 | h) usage ;; |
54 | c) config="$OPTARG" ;; | 54 | c) config="$OPTARG" ;; |
55 | d) dry_run=true ;; | 55 | d) dry_run=true ;; |
56 | k) | 56 | k) |
57 | demolish_tunnels | 57 | demolish_tunnels |
58 | pkill -x mrgrctrnl | 58 | pkill -x mrgrctrnl |
59 | exit | 59 | exit |
60 | ;; | 60 | ;; |
61 | \?) usage 2 ;; | 61 | \?) usage 2 ;; |
62 | *) usage 2 ;; | 62 | *) usage 2 ;; |
63 | esac | 63 | esac |
64 | done | 64 | done |
65 | shift "$((OPTIND - 1))" | 65 | shift "$((OPTIND - 1))" |
66 | 66 | ||
67 | [ -f "$config" ] || die "Need a config! Edit $config" | 67 | [ -f "$config" ] || die "Need a config! Edit $config" |
68 | if [ -e "$pidf" ] && ! "$dry_run" | 68 | if [ -e "$pidf" ] && ! "$dry_run"; then |
69 | then demolish_tunnels | 69 | demolish_tunnels |
70 | fi | 70 | fi |
71 | 71 | ||
72 | awk '{sub(/#.*$/,"");print}' "$config" | | 72 | awk '{sub(/#.*$/,"");print}' "$config" | |
73 | while read -r machine user local remote key rest | 73 | while read -r machine user local remote key rest; do |
74 | do | 74 | if [ -z "$machine" ] || [ -z "$user" ] || |
75 | if [ -z "$machine" ] || [ -z "$user" ] || | 75 | [ -z "$local" ] || [ -z "$remote" ]; then |
76 | [ -z "$local" ] || [ -z "$remote" ] | 76 | continue |
77 | then continue | 77 | fi |
78 | fi | ||
79 | 78 | ||
80 | set -- -N "$user@$machine" -L "$local:$remote" | 79 | set -- -N "$user@$machine" -L "$local:$remote" |
81 | [ -n "$key" ] && set -- "$@" -i "$key" | 80 | [ -n "$key" ] && set -- "$@" -i "$key" |
82 | #shellcheck disable=2086 | 81 | #shellcheck disable=2086 |
83 | [ -n "$rest" ] && set -- "$@" $rest | 82 | [ -n "$rest" ] && set -- "$@" $rest |
84 | 83 | ||
85 | echo ssh "$@" | 84 | echo ssh "$@" |
86 | 85 | ||
87 | "$dry_run" || { | 86 | "$dry_run" || { |
88 | while : | 87 | while :; do |
89 | do | 88 | ssh "$@" |
90 | ssh "$@" | 89 | echo "$!" >>"$pidf" |
91 | echo "$!" >> "$pidf" | 90 | sleep 5 |
92 | sleep 5 | 91 | done & |
93 | done & | 92 | } |
94 | } | 93 | done |
95 | done | ||
96 | 94 | ||
97 | wait | 95 | wait |