shell-base/function/ssh

79 lines
1.9 KiB
Bash

#!/bin/bash
# this will superceed the ssh binary in order to source all the config files
module_load file # loads find and build_file
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 ))
local OUTPUT=${*:-"$BASH_SHELL_BASE/ssh/config/_config"}
[[ $PDIRS ]] && DIRS=($PDIRS) || DIRS=(${BASH_SHELL_DIRS} "$HOME/$BASH_SHELL_USER")
# echo DIRS "${DIRS[@]}"
# echo $OUTPUT
CDIRS=()
j=0
cnt=${#DIRS[@]}
for ((i=0;i<cnt;i++)); do
# echo $i of $cnt
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: $OUTPUT
mkdir -p "$(dirname "$OUTPUT")"
echo -e "$HEADER" > $OUTPUT
build_file "/etc/ssh/ssh_config" $OUTPUT
# echo existing dirs ${CDIRS[@]}
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" $OUTPUT
done
done
build_file "$HOME/.ssh/config" $OUTPUT
}
ssh () {
if [[ $1 = "-F" ]]; then
CONFIG=${2:-"$BASH_SHELL_BASE/ssh/config/_config"}
shift;shift
fi
CONFIG=${CONFIG:-"$BASH_SHELL_BASE/ssh/config/_config"}
[[ -f "$CONFIG" ]] || ssh_config "$CONFIG"
command ssh -F $CONFIG "$@"
}