nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

moshrc (2042B)


      1 #!/usr/bin/env bash
      2 # moshrc is an alternative to sshrc for mosh users
      3 
      4 function moshrc() {
      5 	local SSHHOME=${SSHHOME:=~}
      6 	if [ -f $SSHHOME/.sshrc ]; then
      7 		local files=.sshrc
      8 		if [ -d $SSHHOME/.sshrc.d ]; then
      9 			files="$files .sshrc.d"
     10 		fi
     11 		SIZE=$(tar cfz - -h -C $SSHHOME $files | wc -c)
     12 		if [ $SIZE -gt 65536 ]; then
     13 			echo >&2 $'.sshrc.d and .sshrc files must be less than 64kb\ncurrent size: '$SIZE' bytes'
     14 			exit 1
     15 		fi
     16 		mosh "$@" -- /bin/bash -c "
     17             command -v xxd >/dev/null 2>&1 || { echo >&2 \"sshrc requires xxd to be installed on the server, but it's not. Aborting.\"; exit 1; }
     18             if [ -e /etc/motd ]; then cat /etc/motd; fi
     19             if [ -e /etc/update-motd.d ]; then run-parts /etc/update-motd.d/; fi
     20             export SSHHOME=\$(mktemp -d -t .$(whoami).sshrc.XXXX)
     21             export SSHRCCLEANUP=\$SSHHOME
     22             trap \"rm -rf \$SSHRCCLEANUP; exit\" 0
     23             echo $'$(cat $0 | xxd -p)' | xxd -p -r > \$SSHHOME/moshrc
     24             chmod +x \$SSHHOME/moshrc
     25 
     26             echo $'$(
     27 			cat <<'EOF' | xxd -p
     28 if [ -e /etc/bash.bashrc ]; then source /etc/bash.bashrc; fi
     29 if [ -e ~/.bashrc ]; then source ~/.bashrc; fi
     30 source $SSHHOME/.sshrc;
     31 export PATH=$PATH:$SSHHOME
     32 EOF
     33 		)' | xxd -p -r > \$SSHHOME/sshrc.bashrc
     34 
     35             echo $'$(
     36 			cat <<'EOF' | xxd -p
     37 #!/usr/bin/env bash
     38 exec bash --rcfile <(echo '
     39 if [ -e /etc/bash.bashrc ]; then source /etc/bash.bashrc; fi
     40 if [ -e ~/.bashrc ]; then source ~/.bashrc; fi
     41 source '$SSHHOME'/.sshrc;
     42 export PATH=$PATH:'$SSHHOME'
     43 ') "$@"
     44 EOF
     45 		)' | xxd -p -r > \$SSHHOME/bashsshrc
     46             chmod +x \$SSHHOME/bashsshrc
     47 
     48             echo $'$(tar czf - -h -C $SSHHOME $files | xxd -p)' | xxd -p -r | tar mxzf - -C \$SSHHOME
     49             export SSHHOME=\$SSHHOME
     50             bash --rcfile \$SSHHOME/sshrc.bashrc
     51             "
     52 	else
     53 		echo "No such file: $SSHHOME/.sshrc"
     54 	fi
     55 }
     56 if [ "$1" ]; then
     57 	command -v xxd >/dev/null 2>&1 || {
     58 		echo >&2 "sshrc requires xxd to be installed locally, but it's not. Aborting."
     59 		exit 1
     60 	}
     61 	moshrc "$@"
     62 else
     63 	ssh
     64 fi