myshellconfig/sshs
2024-10-03 13:55:33 +02:00

117 lines
4 KiB
Bash

#!/bin/sh
sshs() {
ENTRY
[ $# -eq 0 ] && { [ "${sshcon:-x}" == "x" ] || set -- "${sshcon}"; }
if [ -n "${TMUX}" ]; then
tmux set-environment sshcon "$@"
fi
# 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=()
# local FILELIST=( "${MSC_BASE}/functions.sh" "${MSC_BASE}/logging" "${MSC_BASE}/myshell_load_fortmpconfig" $(getbashrcfile) ~/.bash_aliases "${MSC_BASE}/aliases" "${MSC_BASE}/PS1" "${MSC_BASE}/bash_completion.d/*" )
echo MSC_BASE: $MSC_BASE
SSHS_ADDFILES="${MSC_BASE}/sshs_addfiles.conf"
echo SSHS_ADDFILES: $SSHS_ADDFILES
if [ -e ${SSHS_ADDFILES} ] ; then
for f in $(envsubst < "${SSHS_ADDFILES}");do
logdebug "add $f to FILELIST"
FILELIST+=("${f}")
done
else
loginfo "Filelist for sshs not found: ${MSC_BASE}/sshs_addfiles.conf"
fi
local SSH_OPTS="-o VisualHostKey=no -o ControlMaster=auto -o ControlPersist=20s"
#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 || { logerr "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}"
trap "rm -f \${bashrctmp} \${vimrctmp} \${SCRIPT_LOG}" EXIT
alias vi='vim -u \${vimrctmp}'
alias vim='vim -u \${vimrctmp}'
alias vimdiff='vimdiff -u \${vimrctmp}'
export LS_OPTIONS="${LS_OPTIONS}"
export VIMRC="\${vimrctmp}"
export BASHRC="\${bashrctmp}"
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 to tmpconfig: $f"
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
#!/bin/bash
bashrctmp=\$(mktemp -p \${XDG_RUNTIME_DIR-~} -t bashrc.XXXXXXXX --suffix=.conf | tr -d '[:space:]')
vimrctmp=\$(mktemp -p \${XDG_RUNTIME_DIR-~} -t vimrc.XXXXXXXX --suffix=.conf | tr -d '[:space:]')
echo "\$bashrctmp \$vimrctmp"
echo "bashrctmp=\${bashrctmp}" > \${bashrctmp}
echo "vimrctmp=\${vimrctmp}" >> \${bashrctmp}
cat <<- 'eof' >> \$bashrctmp
$(cat ${TMPBASHCONFIG})
export SCRIPT_LOG="\$(cat /proc/\$$/cmdline | xargs -0 echo|awk '{print \$3}' |sed 's/.conf$/.log/')"
tmux set-environment bashrctmp ${bashrctmp} &>\${SCRIPT_LOG}
title "\$USER@\$HOSTNAME: \$PWD"
loginfo "This bash runs with temporary config from \$BASHRC"
eof
cat <<- 'eof3' > \${vimrctmp}
$(cat ${MSC_BASE}/vimrc)
eof3
_remote_commands
)
local REMOTETMPCONFIGS=($(ssh -T ${SSH_OPTS} $@ "$sshcmd") )
logdebug "remote tmp bashrc ${REMOTETMPCONFIGS[0]}"
logdebug "remote tmp vimrc ${REMOTETMPCONFIGS[1]}"
ssh -t ${SSH_OPTS} $@ "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
}