From 430c898555cfb3c23b465d8183226693a71c66dc Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 12 Apr 2020 12:26:50 -0500 Subject: Add -k option to kill --- mrgrctrnl | 57 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/mrgrctrnl b/mrgrctrnl index 4f090d1..1229993 100755 --- a/mrgrctrnl +++ b/mrgrctrnl @@ -9,12 +9,13 @@ usage() { cat <<-END mrgrctrnl: make magic ssh tunnels - usage: mrgrctrnl [-h] [-c CONF] [-d] + usage: mrgrctrnl [-h] [-c CONF] [-d] [-k] -h show this help -c CONF use config file CONF. - -d do a dry run: don't actually tunnel . default: \$XDG_CONFIG_HOME/mrgrctrnl/config + -d do a dry run: don't actually tunnel + -k kill all processes and exit END exit "${1:-0}" @@ -31,15 +32,32 @@ die() exit "$ec" } +demolish_tunnels() +{ + printf '%s...' "Demolishing tunnels" + while read -r pid; do + [ -z "$pid" ] && continue + kill "$pid" 2>/dev/null + done < "$pidf" + rm "$pidf" + sleep 3 + printf '%s.\n' "Done" +} + config="${XDG_CONFIG_HOME:=$HOME/.config}/mrgrctrnl/config" pidf=/tmp/mrgrctrnl.pid -dry_run=false; +dry_run=false -while getopts hc:d opt; do +while getopts hc:dk opt; do case "$opt" in h) usage ;; c) config="$OPTARG" ;; d) dry_run=true ;; + k) + demolish_tunnels + pkill -x mrgrctrnl + exit + ;; \?) usage 2 ;; *) usage 2 ;; esac @@ -47,29 +65,28 @@ done shift "$((OPTIND - 1))" [ -f "$config" ] || die "Need a config! Edit $config" -[ -e "$pidf" ] && ! "$dry_run" && { - while read -r pid; do - kill "$pid" - done < "$pidf" - rm "$pidf" -} +if [ -e "$pidf" ] && ! "$dry_run" +then demolish_tunnels +fi awk '{sub(/#.*$/,"");print}' "$config" | while read -r machine user local remote do - [ -z "$machine" ] || [ -z "$user" ] || - [ -z "$local" ] || [ -z "$remote" ] && - continue + if [ -z "$machine" ] || [ -z "$user" ] || + [ -z "$local" ] || [ -z "$remote" ] + then continue + fi echo ssh -N "$user@$machine" -L "$local:$remote" - $dry_run || - while : - do - ssh -N "$user@$machine" -L "$local:$remote" - echo "$!" >> "$pidf" - sleep 3 - done & + "$dry_run" || { + while : + do + ssh -N "$user@$machine" -L "$local:$remote" + echo "$!" >> "$pidf" + sleep 5 + done & + } done wait -- cgit 1.4.1-21-gabe81