#!/bin/bash # config DIALOG=dialog #gdialog kdialog START_DIR="$HOME/.s3h" VERSION="1.8" # TODO: use opt parsing if [[ $# -eq 1 && "$1" == "-v" ]]; then echo "$VERSION" exit 0 fi if [[ $# -eq 1 && -d "$1" ]]; then START_DIR="$1" fi START_DIR=$(realpath "$START_DIR") if [[ ! -d "$START_DIR" ]]; then echo "start directory '$START_DIR' does not exist" >&2 echo -e "either create or set 'start directory' parameter\n" >&2 echo "Usage: $0 [start directory]" >&2 exit 2 fi LAST="$START_DIR/.last" # check if there was a last call, if so fake while loop variable preselect='' if [[ -f "$LAST" ]]; then last=$(cat "$LAST") # check if config file still exists if [[ -f "$last" ]]; then dir=$(dirname "$last") preselect=$(basename "$last") fi fi # select config file configFile='' while [[ -z "$configFile" ]]; do # init dir variable if [[ -z "$dir" ]]; then dir="$START_DIR" fi # relative path to START_DIR # XXX: can be done in bash itself? relPath=$(echo ${dir} |sed "s;^${START_DIR};;g") # add parent selector if not already in $START_DIR if [[ "$START_DIR" == "$dir" ]]; then para=() else para=(".." "<..>") fi # add directory selectors dirs=$(find $dir -mindepth 1 -maxdepth 1 ! -name '.*' -type d -printf "%f\n" |sort) for d in $dirs; do para+=("$d" "<$d>") done # add file selectors files=$(find $dir -mindepth 1 -maxdepth 1 ! -name '.*' -type f -printf "%f\n" |sort) for d in $files; do # text before ~ is tag if [[ "$d" == *"~"* ]]; then tag="${d%%~*}" para+=("$d" "$tag") else para+=("$d" "$d") fi done if [[ ${#para[@]} -eq 0 ]]; then echo "no file or directory found in '$dir'" >&2 echo "either create a connection file like" >&2 echo -e "\ttouch '$dir/root@example.org'" >&2 echo "or create a group by adding a directory" >&2 exit 3 fi # run dialog dialogOptions='' if [[ ! -z "$preselect" ]]; then dialogOptions="$dialogOptions --default-item $preselect" fi ret=$("$DIALOG" --title "${relPath}" --no-tags --stdout $dialogOptions --menu "select connection" 0 0 0 ${para[@]} 2>&1) if [[ $? -ne 0 ]]; then clear exit 1 fi # sepcial handling of parent selector if [[ "$ret" != ".." ]]; then ret="$dir/$ret" if [[ -d "$ret" ]]; then dir=$ret else configFile=$ret fi else dir=$(dirname "$dir") fi done # read config file and connect if [[ -f "$configFile" ]]; then # clear screen // remove dialog colors clear # each line equals one config option para=() while read -r configLine; do para+=($configLine) done < "$configFile" # lookup group specific option file if [[ -f "$dir/.options" ]]; then while read -r configLine; do para+=($configLine) done < "$dir/.options" echo "using group options file '$dir/.options'" fi # save selected configFile for next call of s3h echo "$configFile" > "$LAST" # extract user@host if ~ in config file name echo "config file '$configFile' selected. connecting..." login=$(basename "$configFile") if [[ "$login" == *"~"* ]]; then login="${login#*~}" fi # set console title echo -ne "\033]30;$login\007" # execute ssh echo "Running 'ssh ${para[@]} $login'" ssh ${para[@]} "$login" # reset console title to something usefull echo -ne "\033]30;$USER@${HOSTNAME%%.*}\007" fi