forked from danrabinowitz/sshrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moshrc
executable file
·60 lines (55 loc) · 2.08 KB
/
moshrc
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
#!/usr/bin/env bash
# moshrc is an alternative to sshrc for mosh users
function moshrc() {
local SSHHOME=${SSHHOME:=~}
if [ -f $SSHHOME/.sshrc ]; then
local files=.sshrc
if [ -d $SSHHOME/.sshrc.d ]; then
files="$files .sshrc.d"
fi
SIZE=$(tar cfz - -h -C $SSHHOME $files | wc -c)
if [ $SIZE -gt 65536 ]; then
echo >&2 $'.sshrc.d and .sshrc files must be less than 64kb\ncurrent size: '$SIZE' bytes'
exit 1
fi
mosh "$@" -- /bin/bash -c "
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; }
if [ -e /etc/motd ]; then cat /etc/motd; fi
if [ -e /etc/update-motd.d ]; then run-parts /etc/update-motd.d/; fi
export SSHHOME=\$(mktemp -d -t .$(whoami).sshrc.XXXX)
export SSHRCCLEANUP=\$SSHHOME
trap \"rm -rf \$SSHRCCLEANUP; exit\" 0
echo $'$(cat $0 | xxd -p)' | xxd -p -r > \$SSHHOME/moshrc
chmod +x \$SSHHOME/moshrc
echo $'$( cat << 'EOF' | xxd -p
if [ -e /etc/bash.bashrc ]; then source /etc/bash.bashrc; fi
if [ -e ~/.bashrc ]; then source ~/.bashrc; fi
source $SSHHOME/.sshrc;
export PATH=$PATH:$SSHHOME
EOF
)' | xxd -p -r > \$SSHHOME/sshrc.bashrc
echo $'$( cat << 'EOF' | xxd -p
#!/usr/bin/env bash
exec bash --rcfile <(echo '
if [ -e /etc/bash.bashrc ]; then source /etc/bash.bashrc; fi
if [ -e ~/.bashrc ]; then source ~/.bashrc; fi
source '$SSHHOME'/.sshrc;
export PATH=$PATH:'$SSHHOME'
') "$@"
EOF
)' | xxd -p -r > \$SSHHOME/bashsshrc
chmod +x \$SSHHOME/bashsshrc
echo $'$(tar czf - -h -C $SSHHOME $files | xxd -p)' | xxd -p -r | tar mxzf - -C \$SSHHOME
export SSHHOME=\$SSHHOME
bash --rcfile \$SSHHOME/sshrc.bashrc
"
else
echo "No such file: $SSHHOME/.sshrc"
fi
}
if [ "$1" ]; then
command -v xxd >/dev/null 2>&1 || { echo >&2 "sshrc requires xxd to be installed locally, but it's not. Aborting."; exit 1; }
moshrc "$@"
else
ssh
fi