myshellconfig/sshs
Jakobus Schürz fcacd656e0 function sshs in extra file to avoid troubles
with heredocuments
2022-05-19 16:24:10 +02:00

129 lines
5.7 KiB
Bash

#!/bin/sh
sshs() {
ENTRY
local LOGLEVEL="WARN"
# MKTMPCMD='mktemp $(echo ${XDG_RUNTIME_DIR}/bashrc.XXXXXXXX.conf)'
# VIMMKTMPCMD="mktemp ${XDG_RUNTIME_DIR}/vimrc.XXXXXXXX.conf"
local f
local TMPBASHCONFIG=$(mktemp -p ${XDG_RUNTIME_DIR} -t bashrc.XXXXXXXX --suffix=.conf)
local FILELIST=( "${MSC_BASE}/functions.sh" "${MSC_BASE}/logging" "${MSC_BASE}/myshell_load_fortmpconfig" $(getbashrcfile) ~/.aliases "${MSC_BASE}/aliases" "${MSC_BASE}/PS1" "${MSC_BASE}/bash_completion.d/*" )
logdebug "FILELIST: ${FILELIST[@]}"
if [ -e "${HOME}/.config/myshellconfig/sshs_addfiles.conf" ] ; then
for f in $(cat "${HOME}/.config/myshellconfig/sshs_addfiles.conf");do
[ -e "$f" ] && {\
logdebug "add $f to FILELIST"; \
FILELIST+=("$f"); }
done
fi
logdebug "FILELIST1: ${FILELIST[@]}"
local SSH_OPTS="-o VisualHostKey=no -o ControlMaster=auto -o ControlPersist=20s -o ControlPath=~/.ssh/master-%C"
#local SSH_OPTS="-o VisualHostKey=no -o ControlMaster=yes -o ControlPersist=10s -o ControlPath=~/.ssh/ssh-%C"
# Read /etc/bashrc or /etc/bash.bashrc (depending on distribution) and /etc/profile.d/*.sh first
#ssh -T ${SSH_OPTS} $@ "pwd" >/dev/null 2>/dev/null || { logerror "Server $@ not reachable -> exit"; return 1; }
cat << EOF >> "${TMPBASHCONFIG}"
[ -e /etc/bashrc ] && BASHRC=/etc/bashrc
[ -e /etc/bash.bashrc ] && BASHRC=/etc/bash.bashrc
. \$BASHRC
export USERNAME="${USERNAME}"
export FULLNAME="${FULLNAME}"
export USEREMAIL="${USEREMAIL}"
export SCRIPT_LOG="\$(cat /proc/\$$/cmdline | xargs -0 echo|awk '{print \$3}' |sed 's/.conf$/.log/')"
export LOGLEVEL_DEFAULT="${LOGLEVEL_DEFAULT}"
export FILELOGLEVEL_DEFAULT="${FILELOGLEVEL_DEFAULT}"
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ];then
if [ "$PS1" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
EOF
for f in ${FILELIST[*]}; do
if [ -e $f ]; then
logdebug "add $f to tmpconfig"
cat "$f" >> "${TMPBASHCONFIG}";
fi
done
if [ $# -ge 1 ]; then
if [ -e "${TMPBASHCONFIG}" ] ; then
logdebug "create remote bashrc"
logdebug "SSH_OPTS: $SSH_OPTS"
local sshcmd=$(cat << _remote_commands
mktemp -p \${XDG_RUNTIME_DIR-~} -t bashrc.XXXXXXXX --suffix=.conf | tr -d '[:space:]'
echo " "
mktemp -p \${XDG_RUNTIME_DIR-~} -t vimrc.XXXXXXXX --suffix=.conf | tr -d '[:space:]'
echo " "
_remote_commands
)
local REMOTETMPCONFIGS=($(ssh -T ${SSH_OPTS} $@ "$sshcmd") )
# local REMOTETMPCONFIGS=($(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t bashrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' ) $(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t vimrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' ))
logdebug remote tmp bashrc ${REMOTETMPCONFIGS[0]}
logdebug remote tmp vimrc ${REMOTETMPCONFIGS[1]}
#local REMOTETMPBASHCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t bashrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' )
#logdebug "REMOTETMPBASHCONFIG: $REMOTETMPBASHCONFIG"
# logdebug $(ssh -T ${SSH_OPTS} $@ "stat ${REMOTETMPBASHCONFIG}")
# logdebug $(ssh -T ${SSH_OPTS} $@ "hostnamectl")
#logdebug "create remote vimrc"
#local REMOTETMPVIMCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t vimrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' )
#logdebug "REMOTETMPVIMCONFIG: $REMOTETMPVIMCONFIG"
# Add additional aliases to bashrc for remote-machine
cat << EOF >> "${TMPBASHCONFIG}"
alias vi='vim -u ${REMOTETMPCONFIGS[1]}'
alias vim='vim -u ${REMOTETMPCONFIGS[1]}'
alias vimdiff='vimdiff -u ${REMOTETMPCONFIGS[1]}'
export LS_OPTIONS="${LS_OPTIONS}"
export VIMRC="${REMOTETMPCONFIGS[1]}"
export BASHRC="${REMOTETMPCONFIGS[0]}"
title "\$USER@\$HOSTNAME: \$PWD"
loginfo "This bash runs with temporary config from \$BASHRC"
EOF
logdebug "create fill remote bashrc"
ssh -T ${SSH_OPTs} $@ << _remote_cmd
cat <<- 'eof2' > ${REMOTETMPCONFIGS[0]}
$(cat ${TMPBASHCONFIG})
eof2
cat <<- 'eof3' > ${REMOTETMPCONFIGS[1]}
$(cat ${MSC_BASE}/vimrc)
eof3
_remote_cmd
#echo BLA3
#ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPBASHCONFIG}" < "${TMPBASHCONFIG}"
#ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPCONFIGS[0]}" < "${TMPBASHCONFIG}"
# logtrace $(ssh -T ${SSH_OPTS} $@ "stat ${REMOTETMPBASHCONFIG}")
logdebug "create file remote vimrc"
#ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPVIMCONFIG}" < "${MSC_BASE}/vimrc"
#ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPCONFIGS[1]}" < "${MSC_BASE}/vimrc"
#local RCMD="/bin/bash --noprofile --norc -c "
RCMD="
trap \"rm -f ${REMOTETMPCONFIGS[0]} ${REMOTETMPCONFIGS[1]} ${SCRIPT_LOG}\" EXIT"
# logdebug "run remote shell with temporary config"
# #ssh -t ${SSH_OPTS} $@ "$RCMD; SSHS=true bash -c \"function bash () { /bin/bash --rcfile ${REMOTETMPBASHCONFIG} -i ; } ; export -f bash; exec bash --rcfile ${REMOTETMPBASHCONFIG}\""
# #ssh -t ${SSH_OPTS} $@ "$RCMD; SSHS=true bash --noprofile --norc -c \"function bash () { /bin/bash --rcfile ${REMOTETMPCONFIGS[0]} -i ; } ; export -f bash; exec bash --rcfile ${REMOTETMPCONFIGS[0]}\""
ssh -t ${SSH_OPTS} $@ "$RCMD; SSHS=true exec bash --rcfile ${REMOTETMPCONFIGS[0]} -i "
rm "${TMPBASHCONFIG}"
else
logwarn "${TMPBASHCONFIG} does not exist. Using »ssh -t $@«"
ssh -t "$@"
fi
else
logwarn "too few arguments for sshs" >&2
ssh
fi
EXIT
}