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