-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsshrc
executable file
·156 lines (146 loc) · 4.47 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
# inspired by https://github.com/Russell91/sshrc
SSHHOME=${SSHHOME:=~}
HUMANISM_BASE=${HUMANISM_BASE:="$SSHHOME"}
#CARRYFILES="humanism.sh humanism.test.sh ap.linux-apt ap.osx-brew ap.freebsd-pkg dbg"
CARRYFILES="."
EXCLUDEFILES=$"
--exclude .git
--exclude .gitignore
--exclude test
--exclude issues
--exclude examples
--exclude README.md
--exclude LICENSE
--exclude .prep
--exclude sshrc2
"
#
# CHECK LOCAL REQUIREMENTS
#
LOCALERR=0
if [ -f "$HUMANISM_BASE/humanism.sh" ]; then
if ! command -v tar >/dev/null 2>&1; then
echo >&2 'sshrc: tar required on localhost'
LOCALERR=1
else
SIZE=$(tar czf - $EXCLUDEFILES -h -C $HUMANISM_BASE $CARRYFILES | wc -c)
if [ $SIZE -gt 65536 ]; then
echo >&2 $'sshrc: files must be less than 64kb\ncurrent size: '$SIZE' bytes'
LOCALERR=1
fi
fi
if command -v xxd >/dev/null 2>&1; then
pack_local () {
xxd -p
}
elif command -v od >/dev/null 2>&1; then
pack_local () {
od -v -t x1 | awk '{$1=""; print}' | sed 's/ //g'
# -t x1 for byte order
# -w30 for xxd-r-p compat but -w not support on Unix
}
else
echo >&2 'sshrc: od or xxd required on localhost';
LOCALERR=1
fi
else
echo >&2 "sshrc: no such file $HUMANISM_BASE/humanism.sh"
LOCALERR=1
fi
#
# PACKUP LOCAL ENV
#
PACKEDHUMANISM=""
PACKEDSSHRC=""
if [ $LOCALERR -eq 0 ]; then
PACKEDHUMANISM=$(tar czf - $EXCLUDEFILES -h -C $HUMANISM_BASE $CARRYFILES | pack_local)
PACKEDSSHRC=$(cat "$0" | pack_local)
fi
#
# PREPARE SSH EXEC SCRIPT TO CARRY ENV
#
COPYSCRIPT=$"
CARRY=1
# HUMANISM_BASE wont show until after we login
# so to check if it exists we grep rc files instead
if grep humanism.sh .bashrc >/dev/null 2>&1 || \
grep humanism.sh .profile >/dev/null 2>&1 || \
grep humanism.sh /etc/bashrc >/dev/null 2>&1 || \
grep humanism.sh /etc/profile >/dev/null 2>&1 || \
grep humanism.sh .zshrc >/dev/null 2>&1 || \
grep humanism.sh /etc/zshenv >/dev/null 2>&1; then
echo >&2 'sshrc: humanism found'
CARRY=0
fi
DESIREDSHELL=\$(command -v $(basename $SHELL) || command -v bash || command -v ash || command -v sh)
if [ \$CARRY -eq 1 ]; then
# unpack commands on remote/target
if command -v tar >/dev/null 2>&1; then
if command -v sed >/dev/null 2>&1; then
unpack_remote () {
echo -en \$(echo -n \"\$*\" | tr -d '\n' | sed 's/ //g' | sed 's/../\\\\x&/g')
}
elif command -v xxd >/dev/null 2>&1; then
unpack_remote () {
echo >&2 'remote unpack with xxd'
echo -n \"\$*\" | xxd -r -p
}
else
echo >&2 'sshrc: sed or xxd required on remote';
CARRY=0
fi
else
echo >&2 'sshrc: tar required on remote'
CARRY=0
fi
fi
if [ \$CARRY -eq 0 ]; then
echo >&2 'sshrc: skipping enviornment carry to remote'
#if [ -e /etc/motd ]; then cat /etc/motd; fi
\$DESIREDSHELL
else
#if [ -e /etc/motd ]; then cat /etc/motd; fi
SSHHOME=\$(mktemp -d -t .$(whoami).sshrc.XXXXXX)
if [ \"\$SSHHOME\" == '' ]; then
mkdir ./.humanism
SSHHOME=./.humanism
fi
#export SSHRCCLEANUP=\$SSHHOME
#trap \"rm -rf \$SSHRCCLEANUP; exit\" 0
echo 'sshrc: carry sshrc'
unpack_remote \"$PACKEDSSHRC\" > \$SSHHOME/sshrc \
&& chmod +x \$SSHHOME/sshrc \
&& echo sshrc: carry humanism.tar.gz to \\\"\$SSHHOME\\\" \
&& unpack_remote \"$PACKEDHUMANISM\" | tar xz -C \$SSHHOME -f - \
&& chmod 770 \$SSHHOME \
&& export SSHHOME=\$SSHHOME \
&& echo 'sshrc: create sshrc.bashrc' \
&& PACKEDSSHRCBASHRC=$'"$(
cat << 'EOF' | pack_local
if [ -e /etc/bash.bashrc ]; then source /etc/bash.bashrc; fi
if [ -e ~/.bashrc ]; then source ~/.bashrc; fi
export PATH=$PATH:$SSHHOME
source $SSHHOME/humanism.sh;
if [ -f $SSHHOME/myrc ]; then
source "$SSHHOME/myrc"
fi
EOF
)"' \
&& unpack_remote \"\$PACKEDSSHRCBASHRC\" > \$SSHHOME/sshrc.bashrc \
&& \$DESIREDSHELL --rcfile \$SSHHOME/sshrc.bashrc \
|| ( echo 'humanism.sh carry failed' && \$DESIREDSHELL )
fi
"
#
# MAIN
#
if [ $LOCALERR -eq 0 ]; then
# DEBUG=1 outputs resulting carry blog to /tmp for review
test "$DEBUG" && echo "$COPYSCRIPT" > /tmp/sshrc.debug
# -t force pseudo tty
ssh -t $@ "$COPYSCRIPT"
else
echo >&2 "sshrc: local requirements missing. fall back to normal ssh"
ssh $@
fi