Skip to content

Commit 8d37813

Browse files
author
Russell Stewart
authored
Merge pull request #73 from Russell91/command
Command
2 parents d9cede6 + 7ab2049 commit 8d37813

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

sshrc

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ function sshrc() {
1111
echo >&2 $'.sshrc.d and .sshrc files must be less than 64kb\ncurrent size: '$SIZE' bytes'
1212
exit 1
1313
fi
14-
ssh -t "$@" "
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 "
1526
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; }
16-
if [ -e /etc/motd ]; then cat /etc/motd; fi
17-
if [ -e /etc/update-motd.d ]; then run-parts /etc/update-motd.d/ 2>/dev/null; fi
18-
last -F \$USER 2>/dev/null | grep -v 'still logged in' | head -n1 | awk '{print \"Last login:\",\$4,\$5,\$6,\$7,\$8,\"from\",\$3;}'
27+
$WELCOME_MSG
1928
export SSHHOME=\$(mktemp -d -t .$(whoami).sshrc.XXXX)
2029
export SSHRCCLEANUP=\$SSHHOME
2130
trap \"rm -rf \$SSHRCCLEANUP; exit\" 0
@@ -50,16 +59,39 @@ EOF
5059
5160
echo $'"$(tar czf - -h -C $SSHHOME $files | openssl enc -base64)"' | tr -s ' ' $'\n' | openssl enc -base64 -d | tar mxzf - -C \$SSHHOME
5261
export SSHHOME=\$SSHHOME
62+
echo \"$CMDARG\" >> \$SSHHOME/sshrc.bashrc
5363
bash --rcfile \$SSHHOME/sshrc.bashrc
5464
"
5565
else
5666
echo "No such file: $SSHHOME/.sshrc" >&2
5767
exit 1
5868
fi
5969
}
60-
if [ "$1" ]; then
61-
command -v openssl >/dev/null 2>&1 || { echo >&2 "sshrc requires openssl to be installed locally, but it's not. Aborting."; exit 1; }
62-
sshrc "$@"
63-
else
64-
ssh
65-
fi
70+
71+
function sshrc_parse() {
72+
while [[ -n $1 ]]; do
73+
case $1 in
74+
-b | -c | -D | -E | -e | -F | -I | -i | -L | -l | -m | -O | -o | -p | -Q | -R | -S | -W | -w )
75+
SSHARGS="$SSHARGS $1 $2"; shift ;;
76+
-* )
77+
SSHARGS="$SSHARGS $1" ;;
78+
*)
79+
if [ -z "$DOMAIN" ]; then
80+
DOMAIN="$1"
81+
else
82+
local SEMICOLON=$([[ "$@" = *[![:space:]]* ]] && echo '; ')
83+
CMDARG="$@$SEMICOLON exit"
84+
return;
85+
fi
86+
;;
87+
esac
88+
shift
89+
done
90+
if [ -z $DOMAIN ]; then
91+
ssh $SSHARGS; exit 1;
92+
fi
93+
}
94+
95+
command -v openssl >/dev/null 2>&1 || { echo >&2 "sshrc requires openssl to be installed locally, but it's not. Aborting."; exit 1; }
96+
sshrc_parse "$@"
97+
sshrc

0 commit comments

Comments
 (0)