sshrc (3575B)
1 #!/usr/bin/env bash 2 function sshrc() { 3 local SSHHOME=${SSHHOME:=~} 4 if [ -f $SSHHOME/.sshrc ]; then 5 local files=.sshrc 6 if [ -d $SSHHOME/.sshrc.d ]; then 7 files="$files .sshrc.d" 8 fi 9 SIZE=$(tar cfz - -h -C $SSHHOME $files | wc -c) 10 if [ $SIZE -gt 65536 ]; then 11 echo >&2 $'.sshrc.d and .sshrc files must be less than 64kb\ncurrent size: '$SIZE' bytes' 12 exit 1 13 fi 14 if [ -z "$CMDARG" -a ! -e ~/.sshrc.d/.hushlogin ]; then 15 WELCOME_MSG=" 16 if [ ! -e ~/.hushlogin ]; then 17 if [ -e /etc/motd ]; then cat /etc/motd; fi 18 if [ -e /etc/update-motd.d ]; then run-parts /etc/update-motd.d/ 2>/dev/null; fi 19 last -F \$USER 2>/dev/null | grep -v 'still logged in' | head -n1 | awk '{print \"Last login:\",\$4,\$5,\$6,\$7,\$8,\"from\",\$3;}' 20 fi 21 " 22 else 23 WELCOME_MSG="" 24 fi 25 ssh -t "$DOMAIN" $SSHARGS " 26 command -v openssl >/dev/null 2>&1 || { echo >&2 \"sshrc requires openssl to be installed on the server, but it's not. Aborting.\"; exit 1; } 27 $WELCOME_MSG 28 export SSHHOME=\$(mktemp -d -t .$(whoami).sshrc.XXXX) 29 export SSHRCCLEANUP=\$SSHHOME 30 trap \"rm -rf \$SSHRCCLEANUP; exit\" 0 31 echo $'"$(cat "$0" | openssl enc -base64)"' | tr -s ' ' $'\n' | openssl enc -base64 -d > \$SSHHOME/sshrc 32 chmod +x \$SSHHOME/sshrc 33 echo $'"$( 34 cat <<'EOF' | openssl enc -base64 35 if [ -r /etc/profile ]; then source /etc/profile; fi 36 if [ -r ~/.bash_profile ]; then source ~/.bash_profile 37 elif [ -r ~/.bash_login ]; then source ~/.bash_login 38 elif [ -r ~/.profile ]; then source ~/.profile 39 fi 40 export PATH=$PATH:$SSHHOME 41 source $SSHHOME/.sshrc; 42 EOF 43 )"' | tr -s ' ' $'\n' | openssl enc -base64 -d > \$SSHHOME/sshrc.bashrc 44 echo $'"$( 45 cat <<'EOF' | openssl enc -base64 46 #!/usr/bin/env bash 47 exec bash --rcfile <(echo ' 48 [ -r /etc/profile ] && source /etc/profile 49 if [ -r ~/.bash_profile ]; then source ~/.bash_profile 50 elif [ -r ~/.bash_login ]; then source ~/.bash_login 51 elif [ -r ~/.profile ]; then source ~/.profile 52 fi 53 source '$SSHHOME'/.sshrc; 54 export PATH=$PATH:'$SSHHOME' 55 ') ${@:+"$@"} 56 EOF 57 )"' | tr -s ' ' $'\n' | openssl enc -base64 -d > \$SSHHOME/bashsshrc 58 chmod +x \$SSHHOME/bashsshrc 59 echo $'"$(tar czf - -h -C $SSHHOME $files | openssl enc -base64)"' | tr -s ' ' $'\n' | openssl enc -base64 -d | tar mxzf - -C \$SSHHOME 60 export SSHHOME=\$SSHHOME 61 echo \"$CMDARG\" >> \$SSHHOME/sshrc.bashrc 62 bash --rcfile \$SSHHOME/sshrc.bashrc 63 " 64 else 65 echo "No such file: $SSHHOME/.sshrc" >&2 66 exit 1 67 fi 68 } 69 function sshrc_parse() { 70 while [[ -n $1 ]]; do 71 case $1 in 72 -b | -c | -D | -E | -e | -F | -I | -i | -J | -L | -l | -m | -O | -o | -p | -Q | -R | -S | -W | -w) 73 SSHARGS="$SSHARGS $1 $2" 74 shift 75 ;; 76 -*) 77 SSHARGS="$SSHARGS $1" 78 ;; 79 *) 80 if [ -z "$DOMAIN" ]; then 81 DOMAIN="$1" 82 else 83 local SEMICOLON=$([[ "$@" = *[![:space:]]* ]] && echo '; ') 84 CMDARG="$@$SEMICOLON exit" 85 return 86 fi 87 ;; 88 esac 89 shift 90 done 91 if [ -z $DOMAIN ]; then 92 ssh $SSHARGS 93 exit 1 94 fi 95 } 96 command -v openssl >/dev/null 2>&1 || { 97 echo >&2 "sshrc requires openssl to be installed locally, but it's not. Aborting." 98 exit 1 99 } 100 sshrc_parse "$@" 101 sshrc