change invocation of remote shell in sshs

This commit is contained in:
Jakobus Schürz 2022-05-19 14:22:38 +02:00
parent 0bd8b45e55
commit ec2bd6b3f2

View file

@ -372,115 +372,142 @@ EOF
} }
sshs() { #sshs() {
ENTRY # ENTRY
#
#
local LOGLEVEL="WARN" # local LOGLEVEL="WARN"
# MKTMPCMD='mktemp $(echo ${XDG_RUNTIME_DIR}/bashrc.XXXXXXXX.conf)' ## MKTMPCMD='mktemp $(echo ${XDG_RUNTIME_DIR}/bashrc.XXXXXXXX.conf)'
# VIMMKTMPCMD="mktemp ${XDG_RUNTIME_DIR}/vimrc.XXXXXXXX.conf" ## VIMMKTMPCMD="mktemp ${XDG_RUNTIME_DIR}/vimrc.XXXXXXXX.conf"
#
local f # local f
local TMPBASHCONFIG=$(mktemp -p ${XDG_RUNTIME_DIR} -t bashrc.XXXXXXXX --suffix=.conf) # 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/*" ) # 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[@]}" # logdebug "FILELIST: ${FILELIST[@]}"
if [ -e "${HOME}/.config/myshellconfig/sshs_addfiles.conf" ] ; then # if [ -e "${HOME}/.config/myshellconfig/sshs_addfiles.conf" ] ; then
for f in $(cat "${HOME}/.config/myshellconfig/sshs_addfiles.conf");do # for f in $(cat "${HOME}/.config/myshellconfig/sshs_addfiles.conf");do
[ -e "$f" ] && {\ # [ -e "$f" ] && {\
logdebug "add $f to FILELIST"; \ # logdebug "add $f to FILELIST"; \
FILELIST+=("$f"); } # FILELIST+=("$f"); }
done # done
fi # fi
logdebug "FILELIST1: ${FILELIST[@]}" # 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=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" # #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 # # 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; } # #ssh -T ${SSH_OPTS} $@ "pwd" >/dev/null 2>/dev/null || { logerror "Server $@ not reachable -> exit"; return 1; }
cat << EOF >> "${TMPBASHCONFIG}" # cat << EOF >> "${TMPBASHCONFIG}"
[ -e /etc/bashrc ] && BASHRC=/etc/bashrc #[ -e /etc/bashrc ] && BASHRC=/etc/bashrc
[ -e /etc/bash.bashrc ] && BASHRC=/etc/bash.bashrc #[ -e /etc/bash.bashrc ] && BASHRC=/etc/bash.bashrc
. \$BASHRC #. \$BASHRC
#
export USERNAME="${USERNAME}" #export USERNAME="${USERNAME}"
export FULLNAME="${FULLNAME}" #export FULLNAME="${FULLNAME}"
export USEREMAIL="${USEREMAIL}" #export USEREMAIL="${USEREMAIL}"
export SCRIPT_LOG="\$(cat /proc/\$$/cmdline | xargs -0 echo|awk '{print \$3}' |sed 's/.conf$/.log/')" #export SCRIPT_LOG="\$(cat /proc/\$$/cmdline | xargs -0 echo|awk '{print \$3}' |sed 's/.conf$/.log/')"
export LOGLEVEL_DEFAULT="${LOGLEVEL_DEFAULT}" #export LOGLEVEL_DEFAULT="${LOGLEVEL_DEFAULT}"
export FILELOGLEVEL_DEFAULT="${FILELOGLEVEL_DEFAULT}" #export FILELOGLEVEL_DEFAULT="${FILELOGLEVEL_DEFAULT}"
#
for i in /etc/profile.d/*.sh; do #for i in /etc/profile.d/*.sh; do
if [ -r "$i" ];then # if [ -r "$i" ];then
if [ "$PS1" ]; then # if [ "$PS1" ]; then
. "$i" # . "$i"
else # else
. "$i" >/dev/null # . "$i" >/dev/null
fi # fi
fi # fi
done #done
unset i #unset i
EOF #EOF
#
for f in ${FILELIST[*]}; do # for f in ${FILELIST[*]}; do
if [ -e $f ]; then # if [ -e $f ]; then
logdebug "add $f to tmpconfig" # logdebug "add $f to tmpconfig"
cat "$f" >> "${TMPBASHCONFIG}"; # cat "$f" >> "${TMPBASHCONFIG}";
fi # fi
done # done
#
if [ $# -ge 1 ]; then # if [ $# -ge 1 ]; then
if [ -e "${TMPBASHCONFIG}" ] ; then # if [ -e "${TMPBASHCONFIG}" ] ; then
logdebug "create remote bashrc" # logdebug "create remote bashrc"
logdebug "SSH_OPTS: $SSH_OPTS" # logdebug "SSH_OPTS: $SSH_OPTS"
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:]' )) #
echo remote tmp bashrc ${REMOTETMPCONFIGS[0]} # sshcmd=$(cat << _remote_commands
echo remote tmp vimrc ${REMOTETMPCONFIGS[1]} #mktemp -p \${XDG_RUNTIME_DIR-~} -t bashrc.XXXXXXXX --suffix=.conf | tr -d '[:space:]'
#local REMOTETMPBASHCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t bashrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' ) #echo " "
#logdebug "REMOTETMPBASHCONFIG: $REMOTETMPBASHCONFIG" #mktemp -p \${XDG_RUNTIME_DIR-~} -t vimrc.XXXXXXXX --suffix=.conf | tr -d '[:space:]'
# logdebug $(ssh -T ${SSH_OPTS} $@ "stat ${REMOTETMPBASHCONFIG}") #echo " "
# logdebug $(ssh -T ${SSH_OPTS} $@ "hostnamectl") #mktemp -p \${XDG_RUNTIME_DIR-~} -t tst.XXXXXXXX --suffix=.conf | tr -d '[:space:]'
#logdebug "create remote vimrc" #echo " "
#local REMOTETMPVIMCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t vimrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' ) #_remote_commands
#logdebug "REMOTETMPVIMCONFIG: $REMOTETMPVIMCONFIG" #)
# local REMOTETMPCONFIGS=($(ssh -T ${SSH_OPTS} $@ "$sshcmd") )
# Add additional aliases to bashrc for remote-machine ## 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:]' ))
cat << EOF >> "${TMPBASHCONFIG}" # logdebug remote tmp bashrc ${REMOTETMPCONFIGS[0]}
alias vi='vim -u ${REMOTETMPCONFIGS[1]}' # logdebug remote tmp vimrc ${REMOTETMPCONFIGS[1]}
alias vim='vim -u ${REMOTETMPCONFIGS[1]}' # #local REMOTETMPBASHCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t bashrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' )
alias vimdiff='vimdiff -u ${REMOTETMPCONFIGS[1]}' # #logdebug "REMOTETMPBASHCONFIG: $REMOTETMPBASHCONFIG"
export LS_OPTIONS="${LS_OPTIONS}" ## logdebug $(ssh -T ${SSH_OPTS} $@ "stat ${REMOTETMPBASHCONFIG}")
export VIMRC="${REMOTETMPCONFIGS[1]}" ## logdebug $(ssh -T ${SSH_OPTS} $@ "hostnamectl")
export BASHRC="${REMOTETMPCONFIGS[0]}" # #logdebug "create remote vimrc"
title "\$USER@\$HOSTNAME: \$PWD" # #local REMOTETMPVIMCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR-~} -t vimrc.XXXXXXXX --suffix=.conf" | tr -d '[:space:]' )
loginfo "This bash runs with temporary config from \$BASHRC" # #logdebug "REMOTETMPVIMCONFIG: $REMOTETMPVIMCONFIG"
EOF #
# # Add additional aliases to bashrc for remote-machine
logdebug "create fill remote bashrc" # cat << EOF >> "${TMPBASHCONFIG}"
#ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPBASHCONFIG}" < "${TMPBASHCONFIG}" #alias vi='vim -u ${REMOTETMPCONFIGS[1]}'
ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPCONFIGS[0]}" < "${TMPBASHCONFIG}" #alias vim='vim -u ${REMOTETMPCONFIGS[1]}'
# logtrace $(ssh -T ${SSH_OPTS} $@ "stat ${REMOTETMPBASHCONFIG}") #alias vimdiff='vimdiff -u ${REMOTETMPCONFIGS[1]}'
logdebug "create file remote vimrc" #export LS_OPTIONS="${LS_OPTIONS}"
#ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPVIMCONFIG}" < "${MSC_BASE}/vimrc" #export VIMRC="${REMOTETMPCONFIGS[1]}"
ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPCONFIGS[1]}" < "${MSC_BASE}/vimrc" #export BASHRC="${REMOTETMPCONFIGS[0]}"
local RCMD="/bin/bash --noprofile --norc -c " #title "\$USER@\$HOSTNAME: \$PWD"
RCMD=" #loginfo "This bash runs with temporary config from \$BASHRC"
trap \"rm -f ${REMOTETMPCONFIGS[0]} ${REMOTETMPCONFIGS[1]}\" EXIT " ; #EOF
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}\"" # logdebug "create fill remote bashrc"
ssh -t ${SSH_OPTS} $@ "$RCMD; SSHS=true bash -c \"function bash () { /bin/bash --rcfile ${REMOTETMPCONFIGS[0]} -i ; } ; export -f bash; exec bash --rcfile ${REMOTETMPCONFIGS[0]}\"" # local TESTFILE=/tmp/bla
rm "${TMPBASHCONFIG}" # cat << EOF3 > "${TESTFILE}"
else #ich bin ein testfile
logwarn "${TMPBASHCONFIG} does not exist. Using »ssh -t $@«" #zweite zeile
ssh -t "$@" #EOF3
fi #
else #ssh -T ${SSH_OPTs} $@ << _remote_cmd
logwarn "too few arguments for sshs" >&2 #stat "${REMOTETMPCONFIGS[2]}"
ssh #echo BLA
fi #cat <<- 'eof2' > ${REMOTETMPCONFIGS[2]}
#$(cat ${TMPBASHCONFIG})
EXIT #eof2
} #echo BLA2
#_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]}\" 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 -c \"function bash () { /bin/bash --rcfile ${REMOTETMPCONFIGS[0]} -i ; } ; export -f bash; exec bash --rcfile ${REMOTETMPCONFIGS[0]}\""
# 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
#}
[ -e ${MSC_BASE}/sshs ] && source ${MSC_BASE}/sshs
VIMRC="${MSC_BASE}/vimrc" VIMRC="${MSC_BASE}/vimrc"