From 5579f9853da227590b85b37407d6710975322181 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Sat, 5 Aug 2017 10:08:59 +0300 Subject: [PATCH] ssh-tunnelier inserted --- bin/ssh-tunnelier | 1 + web/ssh-tunnelier | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 120000 bin/ssh-tunnelier create mode 100755 web/ssh-tunnelier diff --git a/bin/ssh-tunnelier b/bin/ssh-tunnelier new file mode 120000 index 0000000..2ffdb95 --- /dev/null +++ b/bin/ssh-tunnelier @@ -0,0 +1 @@ +../web/ssh-tunnelier \ No newline at end of file diff --git a/web/ssh-tunnelier b/web/ssh-tunnelier new file mode 100755 index 0000000..d6f8f72 --- /dev/null +++ b/web/ssh-tunnelier @@ -0,0 +1,116 @@ +#!/bin/bash + +CONFDIR="$HOME/.config/ssh-tunnelier" +CONF="$CONFDIR/config" +MAGIC_TIME=873749328 + +function _helpexit() { + echo "SSH tunnel manager +Runs and monitors preconfigured background ssh tunnels, with the ability +to kill existing ssh processes. You don't need to keep the program on +to remember connected sessions. + +Configuration stored in: $CONF +" + exit 0 +} + +for (( i=1; i<=$#; i++ )); do + [[ ${!i} = "-h" ]] && _helpexit + [[ ${!i} = "--help" ]] && _helpexit +done + + +[[ -f "$CONF" ]] || { + echo No config file. + echo "$CONF" each line like: + echo -L 8888:localhost:8888 servername + mkdir -p "$CONFDIR" + echo -e "# example:\n -L 8888:localhost:8888 servername" > "$CONF" +} +number_re='^[0-9]+$' + +colZ="\033[0m" +colTitle="\033[0;1;32m" +colRow="\033[0;1m" +colMenu="\033[0;33m" +colWarn="\033[0;1;31m" + +function get_id() { + id=$( printf "%s" "$line" | base64 -w 0 ) + echo -n $id +} +function get_command() { + switches=$( echo "$1" | base64 -d ) + echo -n "ssh -f -n $switches \"sleep $MAGIC_TIME; echo tunneler $1\"" +} +function get_pid() { + pgrep -f "$MAGIC_TIME.*echo tunneler $1" | head -n 1 +} +function list_all_ssh() { + printf "\n${colTitle}=============================================\n" + printf "${colTitle} List of SSH:${colZ}\n" + printf "${colTitle}Enter PID to kill, empty to return $colZ\n" + printf "${colTitle}=============================================\n" + printf "${colRow}PID command\n" + pgrep -a -x ssh + read -t 600 inputpid + + [[ "$inputpid" =~ $number_re ]] && { + kill $inputpid + } + +} +function read_config() { + while read line; do + id=$( get_id "$line" ) + pid=$( get_pid "$id" ) + printf "%-3d %-7s %s\n" $i "$pid" "$line" + ids+=( $id ) + i=$(( i + 1 )) + done < <( grep -v ^# "$CONF" | grep '[a-zA-Z]' ) +} +function run_command() { + eval $( get_command "$1" ) +} + +while true; do + ids=() + i=1 + printf "\n${colTitle}=============================================\n" + printf "${colTitle} List of tunnels:${colZ}\n" + printf "${colTitle} (q)uit (e)dit (l)ist ssh [%s]${colZ}\n" $( date +%H:%M ) + printf "${colTitle}=============================================\n" + printf "${colRow}ID PID command\n" + read_config + printf "$colZ" + read -t 600 input + + [[ "$input" = "q" ]] && exit 0 + [[ "$input" = "e" ]] && vim "$CONF" + [[ "$input" = "l" ]] && list_all_ssh + [[ "$input" = "0" ]] && continue + [[ "$input" =~ $number_re ]] && { + j=$(( $input - 1 )) + [[ $j -gt $(( $i - 1 )) ]] && { + printf "${colWarn}No such tunnel number${colZ}\n" + continue + } + + printf "\n${colTitle}Tunnel command: %s${colZ}\n" "$( echo "${ids[$j]}" | base64 -d )" + this_pid="$( get_pid "${ids[$j]}" )" + [[ -n "$this_pid" ]] && { + # PID exists, ask to kill + printf "\n ${colRow}k kill?${colZ}\n" + read -t 600 input2 + [[ "$input2" = "k" ]] && kill $( get_pid "${ids[$j]}" ) + } + [[ -z "$this_pid" ]] && { + # PID empty, run + run_command "${ids[$j]}" + sleep 1 + } + } +done + +