about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCase Duckworth2020-04-12 12:26:50 -0500
committerCase Duckworth2020-04-12 12:26:50 -0500
commit430c898555cfb3c23b465d8183226693a71c66dc (patch)
tree89d54e740f14d9cb0f42827e7acb37c0a7d7f683
parentAdd dry_run flag (diff)
downloadmrgrctrnl-430c898555cfb3c23b465d8183226693a71c66dc.tar.gz
mrgrctrnl-430c898555cfb3c23b465d8183226693a71c66dc.zip
Add -k option to kill
-rwxr-xr-xmrgrctrnl57
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
35demolish_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
34config="${XDG_CONFIG_HOME:=$HOME/.config}/mrgrctrnl/config" 47config="${XDG_CONFIG_HOME:=$HOME/.config}/mrgrctrnl/config"
35pidf=/tmp/mrgrctrnl.pid 48pidf=/tmp/mrgrctrnl.pid
36dry_run=false; 49dry_run=false
37 50
38while getopts hc:d opt; do 51while 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
47shift "$((OPTIND - 1))" 65shift "$((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" && { 68if [ -e "$pidf" ] && ! "$dry_run"
51 while read -r pid; do 69then demolish_tunnels
52 kill "$pid" 70fi
53 done < "$pidf"
54 rm "$pidf"
55}
56 71
57awk '{sub(/#.*$/,"");print}' "$config" | 72awk '{sub(/#.*$/,"");print}' "$config" |
58while read -r machine user local remote 73while read -r machine user local remote
59do 74do
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 }
73done 90done
74 91
75wait 92wait