2022-02-20 13:50:19 -08:00
|
|
|
#!/bin/bash
|
2022-12-30 09:14:39 -08:00
|
|
|
|
2022-02-20 13:50:19 -08:00
|
|
|
# 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() {
|
2022-12-30 09:14:39 -08:00
|
|
|
# echo ssh_config $@
|
2022-02-20 13:50:19 -08:00
|
|
|
local CDIRS
|
|
|
|
local CDIR
|
|
|
|
local DIRS
|
|
|
|
local DIR
|
|
|
|
local PDIRS
|
2022-12-26 10:32:29 -08:00
|
|
|
local RESET
|
|
|
|
local FILE
|
2022-02-20 13:50:19 -08:00
|
|
|
|
|
|
|
declare OPTION
|
|
|
|
declare OPTARG
|
|
|
|
declare OPTIND
|
2022-12-26 10:32:29 -08:00
|
|
|
while getopts 'd:glrf:' OPTION; do
|
2022-02-20 13:50:19 -08:00
|
|
|
# echo $OPTION $OPTARG
|
|
|
|
case "$OPTION" in
|
|
|
|
d)
|
2022-12-26 10:32:29 -08:00
|
|
|
DIRS=$($OPTARG)
|
|
|
|
PDIRS=true
|
2022-02-20 13:50:19 -08:00
|
|
|
# echo option d: $DIRS
|
|
|
|
;;
|
2022-12-26 10:32:29 -08:00
|
|
|
f)
|
|
|
|
# todo if file is relative add $HOME/ssh
|
|
|
|
FILE=$OPTARG
|
|
|
|
;&
|
|
|
|
r)
|
|
|
|
RESET=true
|
|
|
|
rm -f $SSH_CONFIG > /dev/null
|
|
|
|
unset SSH_CONFIG
|
|
|
|
;;
|
|
|
|
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
|
|
|
|
;;
|
2022-02-20 13:50:19 -08:00
|
|
|
*)
|
|
|
|
echo unknown option $OPTION
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
2022-12-26 10:32:29 -08:00
|
|
|
# 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
|
|
|
|
|
2023-01-01 10:43:15 -08:00
|
|
|
[[ ! $DIRS ]] && DIRS=($BASH_SHELL_NETWORKS_LOADED "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV")
|
2022-02-20 13:50:19 -08:00
|
|
|
# echo DIRS "${DIRS[@]}"
|
|
|
|
# echo $SSH_CONFIG
|
|
|
|
CDIRS=()
|
|
|
|
j=0
|
|
|
|
cnt=${#DIRS[@]}
|
|
|
|
for ((i = 0; i < cnt; i++)); do
|
|
|
|
# echo $i of $cnt
|
2022-05-02 10:40:07 -07:00
|
|
|
# looks in ssh/config subdirectory of each DIRS if not passed
|
2022-02-20 13:50:19 -08:00
|
|
|
DIR="${DIRS[i]}$([[ ! $PDIRS ]] && echo /ssh/config)"
|
2022-12-26 10:32:29 -08:00
|
|
|
# echo ----- $i, ${DIRS[i]} trying $DIR
|
2022-02-20 13:50:19 -08:00
|
|
|
[ -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 #
|
|
|
|
##############################################################"
|
|
|
|
|
2022-05-02 10:40:07 -07:00
|
|
|
module_load debug
|
|
|
|
module_load file
|
2022-03-28 10:05:26 -07:00
|
|
|
|
2022-02-20 13:50:19 -08:00
|
|
|
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
|
2022-12-26 10:32:29 -08:00
|
|
|
|
|
|
|
# echo "set -a" > /tmp/config.env
|
|
|
|
> /tmp/config.env
|
|
|
|
|
2022-02-20 13:50:19 -08:00
|
|
|
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
|
2022-12-30 09:14:39 -08:00
|
|
|
# echo "adding ssh config file $f";
|
2022-02-20 13:50:19 -08:00
|
|
|
[[ $f ]] && build_file "$f" $SSH_CONFIG
|
|
|
|
done
|
2022-12-26 10:32:29 -08:00
|
|
|
[[ -f $CDIR/config.env ]] && build_file $CDIR/config.env /tmp/config.env
|
2022-02-20 13:50:19 -08:00
|
|
|
done
|
2022-12-26 10:32:29 -08:00
|
|
|
|
2022-02-20 13:50:19 -08:00
|
|
|
# append any tradtional home config
|
2022-12-30 09:14:39 -08:00
|
|
|
[[ -f "$HOME/.ssh/config" ]] && build_file "$HOME/.ssh/config" $SSH_CONFIG
|
2022-12-26 10:32:29 -08:00
|
|
|
[[ -f "$HOME/.ssh/config.env" ]] && build_file "$HOME/.ssh/config.env" "/tmp/config.env"
|
|
|
|
|
|
|
|
# now replace any variables in file
|
|
|
|
# echo -e "\nset +a" >> /tmp/config.env
|
|
|
|
# cat /tmp/config.env
|
|
|
|
# return
|
|
|
|
# (. /tmp/config.env; envsubst < $SSH_CONFIG) > /tmp/cfg
|
|
|
|
# if [[ $(ssh_config_get x 1> /dev/null) ]]; then
|
|
|
|
# echo error
|
|
|
|
# awk 'NR==193' /tmp/cfg
|
|
|
|
# fi
|
2022-02-20 13:50:19 -08:00
|
|
|
}
|
2022-05-02 10:40:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
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"
|
2022-12-26 10:32:29 -08:00
|
|
|
# echo $@
|
2022-05-02 10:40:07 -07:00
|
|
|
|
|
|
|
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 ]] && { echo must pass a config host; return 1; }
|
|
|
|
|
2022-12-26 10:32:29 -08:00
|
|
|
[[ ! $(cat "$SSH_CONFIG" | grep "[Hh]ost" | grep $1) ]] && echo "no host alias $1" && return 2
|
2022-05-02 10:40:07 -07:00
|
|
|
|
|
|
|
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 ).*")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-26 10:32:29 -08:00
|
|
|
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
|
|
|
|
}
|