diff --git a/web/ssh-tunnelier b/web/ssh-tunnelier index 0875a7e..e56fdb9 100755 --- a/web/ssh-tunnelier +++ b/web/ssh-tunnelier @@ -57,7 +57,7 @@ function fpgrep() { } function get_id() { - id=$( printf "%s" "$line" | $BASE ) + id=$( printf "%s" "$1" | $BASE ) echo -n $id } @@ -108,6 +108,23 @@ function list_all_ssh() { } } +function reset_all_ssh() { + match="ssh.*sleep.*echo tunnelier" + printf "\n${colTitle}=============================================\n" + printf "${colTitle} List of tunneliers:${colZ}\n" + printf "${colTitle}=============================================\n${colRow}" + if [[ -z $( pgrep -u $UID -f "$match" ) ]]; then + echo 'No tunneliers running.' + return + else + pgrep -u $UID -f "$match" | xargs ps -o pid,start,args + fi + printf "\n k kill all\n t terminate all\n empty returns${colZ}\n" + read -t 600 input1 + [[ "$input1" = "k" ]] && pkill -u $UID -f "$match" + [[ "$input1" = "t" ]] && kill -u $UID -9 -f "$match" +} + function read_config() { while read line; do id=$( get_id "$line" ) @@ -119,23 +136,27 @@ function read_config() { } function run_args() { - if [[ -z "$2" ]]; then - # only hostname - if grep -q "^$1:" "$CONF"; then - grep "^$1:" "$CONF" - switches=$( grep "^$1:" "$CONF" | parse_switches ) - else - echo No such host - cat "$CONF" - exit 1 - fi + if [[ -z "$2" ]]; then + # only hostname + if grep -q "^$1:" "$CONF"; then + grep "^$1:" "$CONF" + host="$1" + line=$( grep "^$1:" "$CONF" ) + switches=$( echo "$line" | parse_switches ) else - host="$1" - shift 1 - switches=$( echo "$host: $@" | parse_switches ) + echo No such host + cat "$CONF" + exit 1 fi - eval "ssh -f -n $switches \"nice /bin/bash -c 'for ((i=1;i<$MAGIC_TIME;i++)); do cut -f4 -d \\\" \\\" /proc/\\\$PPID/stat | xargs kill -0 || exit ; sleep 60;done'; echo tunnelier $1\"" - exit $? + else + host="$1" + shift 1 + line="$host: $@" + switches=$( echo "$line" | parse_switches ) + fi + id=$( get_id "$line" ) + eval "ssh -f -n $switches \"nice /bin/bash -c 'for ((i=1;i<$MAGIC_TIME;i++)); do cut -f4 -d \\\" \\\" /proc/\\\$PPID/stat | xargs kill -0 || exit ; sleep 60;done'; echo tunnelier $id\"" + exit $? } function run_command() { @@ -177,7 +198,7 @@ while true; do i=1 printf "\n${colTitle}=============================================\n" printf "${colTitle} List of tunnels: (${LOCALHOSTSYMBOL}=localhost) [%s]${colZ}\n" $( date +%H:%M ) - printf "${colTitle} (q)uit (e)dit (l)ist ssh (i)nstant tunnel${colZ}\n" + printf "${colTitle} (q)uit (e)dit (l)ist (i)nstant (r)eset${colZ}\n" printf "${colTitle}=============================================\n" printf "${colRow}ID PID host: ports\n" read_config @@ -188,6 +209,7 @@ while true; do [[ "$input" = "i" ]] && instant_entry [[ "$input" = "e" ]] && vim "$CONF" [[ "$input" = "l" ]] && list_all_ssh + [[ "$input" = "r" ]] && reset_all_ssh [[ "$input" = "0" ]] && continue [[ "$input" =~ $number_re ]] && { j=$(( $input - 1 ))