-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpushSshKeys.bash
More file actions
executable file
·163 lines (121 loc) · 3.32 KB
/
Copy pathpushSshKeys.bash
File metadata and controls
executable file
·163 lines (121 loc) · 3.32 KB
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
157
158
159
160
161
162
#!/bin/bash
prog="$(basename $0)"
# gnome-keyring BUG:
# gnome-keyring was trying to handle all SSH key usage, preventing
# the keys from working.
# see: https://chrisjean.com/ubuntu-ssh-fix-for-agent-admitted-failure-to-sign-using-the-key/
# Setting evn: SSH_AUTH_SOCK=0 can stop ssh from using the gnome-keyring
function usage()
{
cat << EOF || exit $?
Usage: $prog TO_HOST [-f FROM_HOST] [ssh_options]
Generate ssh public/private rsa key pair (if needed) and put
the public key on the machine at TO_HOST, so that you may ssh to
TO_HOST from the current machine FROM_HOST without a password.
If FROM_HOST is not given the output of 'hostname -s' will be
used as the host name we ssh from.
If ssh_options are given they will be the first arguments to the
ssh calls. Example:
$prog workhorse -f rider -p 4554
References:
This follows something like what you see at:
http://www.linuxproblem.org/art_9.html
Or google search:
ssh without password
BUGS:
The gnome-keyring can interfer with ssh in general. You can
disable the gnome-keyring by setting environment variable
SSH_AUTH_SOCK=0, so for example in a bash shell run:
unset SSH_AUTH_SOCK
Security-Enhanced Linux (SELinux) can do a good job of keeping
sshing with keys from working by limiting access to .ssh/ files.
EOF
exit 1
}
[ -z "$1" ] && usage
tohost=
sshoptions=" "
fromhost="$(hostname -s)" || exit 1
who="$(whoami)"
tohost="$1"
shift 1
while [ -n "$1" ] ; do
case "$1" in
--help|-h)
usage
;;
-f)
shift 1
[ -z "$1" ] && usage
fromhost="$1"
;;
*)
sshoptions="${sshoptions}$1 "
;;
esac
shift 1
done
function checkLocalKeys()
{
set +x
if [ ! -f "${HOME}/.ssh/id_rsa" ] ; then
echo
echo "generating ssh keys ..."
set -x
echo -e "\n\n\n\n\n\n\n\n" |\
ssh-keygen -t rsa -N '' || exit $?
set +x
else
echo
echo "Your local ssh keys exist in ${HOME}/.ssh/id_rsa, which is fine."
fi
if [ ! -f "${HOME}/.ssh/id_rsa" ] ; then
echo
echo "failed to generate ssh keys"
echo
exit 1
fi
}
function success()
{
set +x
echo "$prog was successful"
exit 0
}
function checkWorksAlready()
{
set -x
if ssh ${sshoptions}-o 'StrictHostKeyChecking no' -o\
'PreferredAuthentications=publickey' $1 'echo "It worked"'
set +x
echo -e "\nLooks like you can ssh to $1 without a password already\n" ; then
success
fi
}
function rmHostFromKnown()
{
set -x
if ! ssh-keygen -R $1 ; then
set +x
echo "failed to remove host $1 from .ssh/known_hosts"
echo "but that may be because you do not know them yet"
echo
else
set +x
fi
}
function pushPublicKeyToHyHost()
{
set -x
cat ${HOME}/.ssh/id_rsa.pub | ssh ${sshoptions}-o 'StrictHostKeyChecking no' $1\
'mkdir -p ${HOME}/.ssh && cat >> ${HOME}/.ssh/authorized_keys && chmod 600 ${HOME}/.ssh/authorized_keys && chmod 700 ${HOME}/.ssh'\
|| exit $?
set +x
}
# disable the gnome-keyring
export SSH_AUTH_SOCK=0
checkLocalKeys "${fromhost}"
checkWorksAlready ${tohost}
rmHostFromKnown ${tohost}
pushPublicKeyToHyHost ${tohost}
success