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.
master
Kebler Network System Administrator 2021-02-05 07:18:26 -08:00
parent 867875d413
commit 460aecc93b
8 changed files with 133 additions and 36 deletions

View File

@ -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

86
all/modules/ssh.sh Normal file
View File

@ -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 "$@"
}

38
all/modules/sshfs.sh Executable file
View File

@ -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
}

View File

@ -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
}

1
all/ssh/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/_config

3
all/ssh/config/readme.md Normal file
View File

@ -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

View File

@ -0,0 +1,3 @@
if [[ $- == *i* ]]; then
echo ssh interactive session
fi

View File

@ -0,0 +1 @@
*anything in /session will be sourced if this is a remote ssh login session*