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