shell-network/modules/ssh-config.mod

217 lines
5.4 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() {
# echo ssh_config $@
local CDIRS
local CDIR
local DIRS
local DIR
local PDIRS
local RESET
local FILE
local USEENV
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'd:glrf:e' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
d)
DIRS=$($OPTARG)
PDIRS=true
# echo option d: $DIRS
;;
f)
# todo if file is relative add $HOME/ssh
FILE=$OPTARG
;&
r)
RESET=true
rm -f $SSH_CONFIG > /dev/null
unset SSH_CONFIG
;;
# use environment when sustituting
e)
USEENV=-e
;;
g)
shift 1
ssh_config_get $@
return $?
;;
l)
shift 1
if [[ $1 ]]; then
cat $SSH_CONFIG | grep -F "$1"
else
cat $SSH_CONFIG
fi
echo -e "\n"
return 1
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $((OPTIND - 1))
# if more arguments passsed then just get the entry otherwise build the file
[[ $1 ]] && ssh_config_get $@ && return $?
if [[ (! $SSH_CONFIG || $RESET ) ]]; then
[[ -f $SSH_CONFIG ]] && rm "$SSH_CONFIG"
SSH_CONFIG=${FILE:-$SSH_CONFIG}
export SSH_CONFIG=${SSH_CONFIG:-"$HOME/.ssh/_config"}
fi
if [[ ! -f $SSH_CONFIG ]]; then
mkdir -p "$(dirname "$SSH_CONFIG")"
touch $SSH_CONFIG
chmod 600 $SSH_CONFIG
fi
[[ ! $DIRS ]] && DIRS=($BASH_SHELL_NETWORKS_LOADED "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV")
# 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 if not passed
DIR="${DIRS[i]}$([[ ! $PDIRS ]] && echo /ssh/config)"
# echo ----- $i, ${DIRS[i]} 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 #
##############################################################"
module_load debug
module_load file
module_load env
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
# empty temp file
> /tmp/${USER}-config.env
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 "adding ssh config file $f";
[[ $f ]] && build_file "$f" $SSH_CONFIG
done
[[ -f $CDIR/config.env ]] && build_file $CDIR/config.env /tmp/${USER}-config.env
done
# append any tradtional home config
[[ -f "$HOME/.ssh/config" ]] && build_file "$HOME/.ssh/config" $SSH_CONFIG
[[ -f "$HOME/.ssh/config.env" ]] && build_file "$HOME/.ssh/config.env" "/tmp/${USER}-config.env"
# echo $SSH_CONFIG
# cat $SSH_CONFIG
# now replace any variables in file
# echo sub env
# cat /tmp/${USER}-config.env
env_subs_file $USEENV -o $SSH_CONFIG "/tmp/${USER}-config.env"
rm -f /tmp/${USER}-config.env
}
ssh_config_get () {
local ssh;
local cfg;
local prop;
local all;
if [[ $SSH_CONFIG ]]; then
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
cfg="-F $SSH_CONFIG"
fi
ssh="$(which ssh) $cfg -G"
# echo ssh cmd: "$ssh"
# echo $@
local OPTION; local OPTARG; local OPTIND
while getopts 'uhpai' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
u)
prop=user
;;
h)
prop=hostname
;;
i)
prop=identityfile
;;
p)
prop=port
;;
a)
all=true
;;
*) echo unknown run option -$OPTARG
return 3
;;
esac
done
shift $((OPTIND - 1))
[[ ! $1 ]] && { >&2 echo must pass a config host; return 1; }
[[ ! $(cat "$SSH_CONFIG" | grep "[Hh]ost" | grep $1) ]] && >&2 echo "no host alias $1" && return 2
props=$($ssh $1)
[[ $all ]] && { echo "$props"; return 0; }
[[ $prop ]] && { echo "$props" | grep -m1 -oP "(?<=$prop ).*"; return 0; }
[[ $2 ]] && { echo "$props" | grep $2; return 0; }
echo host $1
echo user $(echo "$props" | grep -m1 -oP "(?<=user ).*")
echo hostname $(echo "$props" | grep -m1 -oP "(?<=hostname ).*")
echo port $(echo "$props" | grep -m1 -oP "(?<=port ).*")
echo identityfile $(echo "$props" | grep -m1 -oP "(?<=identityfile ).*")
}
# ssh_config_env () {
# SSH_CONFIG_ENV={$SSH_CONFIG_ENV:-$HOME/.ssh/config.env}
# SSH_CONFIG_TEMP={$SSH_CONFIG_TEMP:-$HOME/.ssh/.config}
# [[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
# if [[ -f $SSH_CONFIG ]]; then
# env=[[ -f $SSH_CONFIG_ENV ]] && echo $SSH_CONFIG_ENV || echo ""
# env_merge_file $SSH_CONFIG $SSH_CONFIG_ENV > $SSH_CONFIG_TEMP
# cfg="-F $SSH_CONFIG_TEMP"
# cat $SSH_CONFIG_TEMP | grep -a6 newbox
# fi
# return
# }