myshellconfig/sshs

118 lines
4 KiB
Text
Raw Permalink Normal View History

#!/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=()
2023-04-09 23:51:54 +02:00
# 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/*" )
2023-04-09 23:51:54 +02:00
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
2023-01-27 16:43:30 +01:00
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
2024-10-03 13:55:33 +02:00
#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}"
2022-06-27 02:39:41 +02:00
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
2022-06-27 02:39:41 +02:00
#!/bin/bash
2024-09-26 18:07:06 +02:00
2022-06-27 02:39:41 +02:00
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"
2022-06-27 02:39:41 +02:00
echo "bashrctmp=\${bashrctmp}" > \${bashrctmp}
echo "vimrctmp=\${vimrctmp}" >> \${bashrctmp}
cat <<- 'eof' >> \$bashrctmp
2024-09-26 18:07:06 +02:00
2022-06-27 02:39:41 +02:00
$(cat ${TMPBASHCONFIG})
2024-09-26 18:07:06 +02:00
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"
2022-06-27 02:39:41 +02:00
eof
2022-06-27 02:39:41 +02:00
cat <<- 'eof3' > \${vimrctmp}
$(cat ${MSC_BASE}/vimrc)
eof3
2022-06-27 02:39:41 +02:00
_remote_commands
)
local REMOTETMPCONFIGS=($(ssh -T ${SSH_OPTS} $@ "$sshcmd") )
2024-09-26 18:07:06 +02:00
logdebug "remote tmp bashrc ${REMOTETMPCONFIGS[0]}"
logdebug "remote tmp vimrc ${REMOTETMPCONFIGS[1]}"
2022-06-27 02:39:41 +02:00
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
}