feat: refactor ssh-config to enable environment variable substition

master
Kebler Network System Administrator 2022-12-26 10:32:29 -08:00
parent d9fa3b6c74
commit e205bf3c50
7 changed files with 102 additions and 112 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/archive/

2
env/backup.env vendored
View File

@ -1,2 +0,0 @@
# set this if you have all networks common backup server api
# export BACKUP_SERVER=https://backup.xxxx.net

8
function/net-utils.sh Normal file
View File

@ -0,0 +1,8 @@
publicip () {
dig +short myip.opendns.com @resolver1.opendns.com
}
getip () {
dig +short $1 | tail -1
}

View File

@ -2,25 +2,50 @@
# 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_ANY_NETWORK/ssh/_config"
function ssh_config() {
local CDIRS
local CDIR
local DIRS
local DIR
local PDIRS
local RESET
local FILE
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'd:' OPTION; do
while getopts 'd:glrf:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
d)
PDIRS=$OPTARG
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
;;
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
;;
@ -29,7 +54,24 @@ function ssh_config() {
shift $((OPTIND - 1))
[[ $PDIRS ]] && DIRS=($PDIRS) || DIRS=(${BASH_SHELL_DIRS} "$HOME/$BASH_SHELL_USER")
# 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_NETWORK_DIRS "$BASH_SHELL_HOST" "$HOME/$BASH_SHELL_USER" "$BASH_SHELL_DEV")
# echo DIRS "${DIRS[@]}"
# echo $SSH_CONFIG
CDIRS=()
@ -39,7 +81,7 @@ function ssh_config() {
# echo $i of $cnt
# looks in ssh/config subdirectory of each DIRS if not passed
DIR="${DIRS[i]}$([[ ! $PDIRS ]] && echo /ssh/config)"
# echo ----- trying $DIR
# echo ----- $i, ${DIRS[i]} trying $DIR
[ -d $DIR ] && CDIRS[j]=$DIR
j+=1 || echo no directory $DIR
done
@ -56,23 +98,38 @@ function ssh_config() {
module_load debug
module_load file
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
# echo "set -a" > /tmp/config.env
> /tmp/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 "Processing $f";
[[ $f ]] && build_file "$f" $SSH_CONFIG
done
[[ -f $CDIR/config.env ]] && build_file $CDIR/config.env /tmp/config.env
done
# append any tradtional home config
[[ -f "$HOME/.ssh/config" ]] && build_file "$HOME/.ssh/config" $SSH_CONFIG
[[ -f "$HOME/.ssh/config" ]] && build_file -l "$HOME/.ssh/config" $SSH_CONFIG
[[ -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
}
@ -90,6 +147,7 @@ ssh_config_get () {
ssh="$(which ssh) $cfg -G"
# echo ssh cmd: "$ssh"
# echo $@
local OPTION; local OPTARG; local OPTIND
while getopts 'uhpai' OPTION; do
@ -120,7 +178,7 @@ ssh_config_get () {
[[ ! $1 ]] && { echo must pass a config host; return 1; }
[[ ! $(cat "$SSH_CONFIG" | grep "[Hh]ost $1") ]] && return 2
[[ ! $(cat "$SSH_CONFIG" | grep "[Hh]ost" | grep $1) ]] && echo "no host alias $1" && return 2
props=$($ssh $1)
[[ $all ]] && { echo "$props"; return 0; }
@ -135,3 +193,19 @@ ssh_config_get () {
}
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
}

View File

@ -17,18 +17,19 @@
# > String::split ret "$(ssh -p f filename -m -r test -p 32)"
# > host=${ret[0]}; opts=${ret[1]};sshpass=${ret[2]}
# module_load file
module_load net-utils
module_load ssh-config
ssh() {
local pw;local cfg;local opts;local mp; local sshpass; local dr
local host; local user; local script; local ret ; local key
local pw;local cfg;local opts;local mp; local sshpass; local dr; local sshcmd
local host; local user; local script; local ret ; local key; local efile; local tfile
if [[ $SSH_CONFIG ]]; then
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
cfg="-F $SSH_CONFIG"
fi
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
cfg="-F $SSH_CONFIG"
fi
# echo passed: $*
@ -131,8 +132,8 @@ if [[ $ret ]]; then
# run remote commands right here
sshcmd="$sshpass $(which ssh) $opts $host"
# echo extra args: "$@"
# echo running command: $ "$sshcmd"
[[ ! $dr ]] && $sshcmd "$@" || echo $sshcmd "$@"
# echo running command: "$sshcmd"
[[ ! $dr ]] && $sshcmd "$@" || echo SSH Command Failed: $sshcmd "$@"
fi
} # end ssh

View File

@ -1,92 +0,0 @@
#!/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=$SSH_CONFIG
local PORT=22
declare SSHOPTS
declare OPTION
declare OPTARG
declare OPTIND
declare MNTUSER
while getopts 'u:np:o:F:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
p)
PORT=$OPTARG
# echo option d: $DIRS
;;
u)
MNTUSER=$OPTARG
;;
n)
MNTUSER=_NONE_
;;
o)
echo "adding sshfs option: $OPTARG"
SSHOPTS="$SSHOPTS -o $OPTARG"
;;
F)
echo "using SSH Config file at: $OPTARG"
CONFIG=$OPTARG
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $((OPTIND - 1))
# first item is nowsource, second is local mount point, third is possbile local user
HOST=$(sed 's/.*@\(.*\):.*/\1/' <<<"$1")
[[ ! $(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
mkdir -p $2
# can add any options after mount point directory like -o default_permissions
config=$([[ -f $CONFIG ]] && echo "-F $CONFIG")
if [[ ! $MNTUSER == "_NONE_" ]]; then
MNTUSER=${MNTUSER:-$USER}
id=$(id -u ${MNTUSER})
if [[ $id ]]; then
SSHOPTS="$SSHOPTS -o uid=$id -o allow_other"
else
echo no user ${MNTUSER} on this machine, aborting mount
return 1
fi
else
MNTUSER=""
fi
args="-p $PORT $SSHOPTS $config $1 $2"
echo SSHFS $([[ $MNTUSER ]] && echo mounted as user ${MNTUSER}): $args
sshfs $args
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
}
function mntBackup() {
smount root@$1:/backup /backup/remote -p 22 -o allow_other
}
function umntBackup() {
usmount /backup/remote
}