From 460aecc93b3885feb15c6293c7f99c2b55f1e767 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Fri, 5 Feb 2021 07:18:26 -0800 Subject: [PATCH] moved ssh stuff from base to network repo move ssh function to module and load via modulue load refactor ssh_config to suppor SSH_CONFIG env var sshfs smount will load SSH_CONFIG with sshfs if available which can fix issues with not finding the best key immediately. --- all/function/ssh | 6 +-- all/modules/ssh.sh | 86 +++++++++++++++++++++++++++++++++++++ all/modules/sshfs.sh | 38 ++++++++++++++++ all/modules/sshfs/sshfs.sh | 31 ------------- all/ssh/.gitignore | 1 + all/ssh/config/readme.md | 3 ++ all/ssh/session/interactive | 3 ++ all/ssh/session/readme.md | 1 + 8 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 all/modules/ssh.sh create mode 100755 all/modules/sshfs.sh delete mode 100755 all/modules/sshfs/sshfs.sh create mode 100644 all/ssh/.gitignore create mode 100644 all/ssh/config/readme.md create mode 100644 all/ssh/session/interactive create mode 100644 all/ssh/session/readme.md diff --git a/all/function/ssh b/all/function/ssh index a68f4e0..f01e7c8 100644 --- a/all/function/ssh +++ b/all/function/ssh @@ -1,5 +1 @@ -#!/bin/bash -# remote start a program (with x11 forwarding will render locally if gui) -function rrem(){ - ssh -X -t "$1" """$2" "$3""" -} +module_load ssh diff --git a/all/modules/ssh.sh b/all/modules/ssh.sh new file mode 100644 index 0000000..3643b03 --- /dev/null +++ b/all/modules/ssh.sh @@ -0,0 +1,86 @@ +#!/bin/bash +# this will superceed the ssh binary in order to source all the config files +module_load file # loads find and build_file + +[[ ! $SSH_CONFIG ]] && export SSH_CONFIG="$BASH_SHELL_NETWORK/all/ssh/_config" + +function ssh_config() { + local CDIRS + local CDIR + local DIRS + local DIR + local PDIRS + + declare OPTION + declare OPTARG + declare OPTIND + while getopts 'd:' OPTION; do + # echo $OPTION $OPTARG + case "$OPTION" in + d) + PDIRS=$OPTARG + # echo option d: $DIRS + ;; + *) + echo unknown option $OPTION + ;; + esac + done + + shift $((OPTIND - 1)) + + [[ $PDIRS ]] && DIRS=($PDIRS) || DIRS=(${BASH_SHELL_DIRS} "$HOME/$BASH_SHELL_USER") + # echo DIRS "${DIRS[@]}" + # echo $SSH_CONFIG + CDIRS=() + j=0 + cnt=${#DIRS[@]} + for ((i = 0; i < cnt; i++)); do + # echo $i of $cnt + # looks in ssh/config subdirectory of each DIRS is not passed + DIR="${DIRS[i]}$([[ ! $PDIRS ]] && echo /ssh/config)" + # echo ----- trying $DIR + [ -d $DIR ] && CDIRS[j]=$DIR + j+=1 || echo no directory $DIR + done + # CDIRS=("${CDIRS[@]}") + # echo ${CDIRS[@]} + + local HEADER="############################################################## +# THIS FILE IS GENERATED BY function ssh_config. Do not edit # +# It is created by combination of ssh configuration files # +# which are listed in a comment line before each # +# It is used by the ssh function which then calls ssh binary # +##############################################################" + + debug ssh config file at: $SSH_CONFIG + mkdir -p "$(dirname "$SSH_CONFIG")" + echo -e "$HEADER" >$SSH_CONFIG + # build_file appends the given file to output file cleanly with checks + # append any system config + build_file "/etc/ssh/ssh_config" $SSH_CONFIG + # echo existing dirs ${CDIRS[@]} + # will append any .cfg file found in ssh/config subdir of any BASH_SHELL_DIRS, including home shell + for CDIR in "${CDIRS[@]}"; do + # FILES=$(find -n '*.cfg' -d 0 $CDIR) + for f in $(_find -n '*.cfg' -p 'archive off' -d 0 $CDIR); do + # echo "Processing $f"; + [[ $f ]] && build_file "$f" $SSH_CONFIG + done + done + # append any tradtional home config + build_file "$HOME/.ssh/config" $SSH_CONFIG +} + +ssh() { + if [[ $SSH_CONFIG ]]; then + [[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG" + command ssh -F $SSH_CONFIG "$@" + else + command ssh "$@" + fi +} + +function rrem() { + ssh -X -t "$@" +} diff --git a/all/modules/sshfs.sh b/all/modules/sshfs.sh new file mode 100755 index 0000000..616bb33 --- /dev/null +++ b/all/modules/sshfs.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# depends on sshfs fuse for ssh +module_load filesystem # mounted +module_load net-utils # host_reachable +module_load ssh + +function smount() { + + local HOST + local PORT + local config + HOST=$(sed 's/.*@\(.*\):.*/\1/' <<<"$1") + # TODO sed search for -p and extract port, below assumes 3 and 4 position + PORT=$([[ $3 = "-p" ]] && echo $4 || echo 22) + [[ ! $(host_reachable $HOST $PORT) ]] && echo host $HOST not reachable, aborting mount && return 1 + if [[ $(mounted $2) ]]; then + echo "aborting mount: $1 already mounted at $2" + else + echo "SSHFS: mounting $1 at $2" + mkdir -p $2 + # can add any options after mount point directory like -o default_permissions + if [[ $SSH_CONFIG ]]; then + [[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG" + config=$([[ -f $SSH_CONFIG ]] && echo "-F $SSH_CONFIG") + fi + # echo sshfs "$*" "$config" + sshfs $* $config + fi +} + +function usmount() { + if [[ $(mounted $1) ]]; then + echo "unmounting remote file system at $1" + fusermount -u $1 + else + echo "nothing mounted at $1, aborting unmount" + fi +} diff --git a/all/modules/sshfs/sshfs.sh b/all/modules/sshfs/sshfs.sh deleted file mode 100755 index f5e686d..0000000 --- a/all/modules/sshfs/sshfs.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash -# depends on sshfs fuse for ssh -module_load filesystem # mounted -module_load net-utils # host_reachable - -function smount(){ -local HOST -local PORT -HOST=$(sed 's/.*@\(.*\):.*/\1/' <<< "$1") -# TODO search for -p and extract port, this assumes 3 and 4 position -PORT=$([[ $3 = "-p" ]] && echo $4 || echo 22) -[[ ! $(host_reachable $HOST $PORT) ]] && echo host $HOST not reachable, aborting mount && return 1 -if [[ $(mounted $2) ]]; then -echo "remote $1 already mounted at $2, aborting mount" -else - echo "mounting via ssh" - echo sshfs "$*" - mkdir -p $2 - # can add any options after mount point directory like -o default_permissions - sshfs "$@" -fi -} - -function usmount(){ -if [[ $(mounted $1) ]]; then - echo "unmounting remote file system at $1" - fusermount -u $1 -else - echo "nothing mounted at $1, aborting unmount" -fi -} diff --git a/all/ssh/.gitignore b/all/ssh/.gitignore new file mode 100644 index 0000000..b3483f8 --- /dev/null +++ b/all/ssh/.gitignore @@ -0,0 +1 @@ +/_config diff --git a/all/ssh/config/readme.md b/all/ssh/config/readme.md new file mode 100644 index 0000000..689e50b --- /dev/null +++ b/all/ssh/config/readme.md @@ -0,0 +1,3 @@ + +all files in this ssh/config subdirectory will be incorporated into a master ssh configuration per the ssh_config function in the ssh module + diff --git a/all/ssh/session/interactive b/all/ssh/session/interactive new file mode 100644 index 0000000..fc790db --- /dev/null +++ b/all/ssh/session/interactive @@ -0,0 +1,3 @@ +if [[ $- == *i* ]]; then +echo ssh interactive session +fi diff --git a/all/ssh/session/readme.md b/all/ssh/session/readme.md new file mode 100644 index 0000000..223d2e1 --- /dev/null +++ b/all/ssh/session/readme.md @@ -0,0 +1 @@ +*anything in /session will be sourced if this is a remote ssh login session*