-
Notifications
You must be signed in to change notification settings - Fork 2
/
nopw
executable file
·67 lines (54 loc) · 2.04 KB
/
nopw
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
#!/bin/bash
# Enables password-less ssh TO username@remotehost FROM localhost.
if [[ -z $1 ]] || echo "$*" | grep -Eq -- '--help\b|-h\b'; then
echo "usage: ${0##*/} [-h|-help] [ssh connection options] username@remotehost" 1>&2
echo "Puts your public ssh key in the authorized_keys on the remote host."
exit 1
fi
# shellcheck source=.shell_control
# shellcheck disable=1091
source "$HOME/.shell_control" || echo "$(tput bold)error: ~/.shell_control not installed!$(tput sgr0)" >&2
KEY_TYPE="${KEY_TYPE:-rsa}"
KEY="${KEY:-id_$KEY_TYPE}"
echo
show "*** KEY_TYPE set to ${KEY_TYPE} ***"
show "*** KEY set to ${KEY} ***"
echo
run_local() {
echo $'\e[1;34mlocal ==>\e[0m\e[31m' "$1" $'\e[0m'
eval "$1"
return $?
}
set -e
if [[ ! -f "$HOME/.ssh/$KEY.pub" ]]; then
run_local "ssh-keygen -t '$KEY_TYPE'"
run_local "cat '$HOME/.ssh/$KEY.pub' >> '$HOME/.ssh/authorized_keys'"
fi
# SSH requires certain permissions on home directory, .ssh directory, and authorized_keys file
run_local "chmod a-rw,a+x,u+rw $HOME"
[[ -d "$HOME/.ssh" ]] || run_local "mkdir -p '$HOME/.ssh'"
run_local "chmod a-rwx,u+rwx $HOME/.ssh"
[[ -f "$HOME/.ssh/authorized_keys" ]] || run_local "touch '$HOME/.ssh/authorized_keys'"
run_local "chmod a-rwx,u+rw $HOME/.ssh/authorized_keys"
echo
show "*** You will be asked for your password twice... ***"
echo
run_local "cat '$HOME/.ssh/$KEY.pub' | ssh $* 'cat > \"$KEY.pub\"'"
# Don't quote EOF here, we want variable expansion locally here.
# shellcheck disable=SC2087
ssh "$@" "bash -s" <<EOF
remote_run() {
echo $'\\e[1;34mremote ==>\\e[0m\\e[31m' "\$1" $'\\e[0m'
eval "\$1"
return \$?
}
set -e
remote_run "cd ~"
remote_run "chmod go-w,a-rw,a+x,u+rw ."
[[ -d ".ssh" ]] || remote_run "mkdir -p .ssh"
remote_run "chmod a-rwx,u+rwx .ssh"
[[ -f ".ssh/authorized_keys" ]] || remote_run "touch .ssh/authorized_keys"
remote_run "chmod a-rwx,u+rw .ssh/authorized_keys"
remote_run "cat '$KEY.pub' >> .ssh/authorized_keys"
[[ -f "$KEY.pub" ]] && remote_run "rm '$KEY.pub'"
EOF