-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassless-auth
executable file
·60 lines (51 loc) · 1.04 KB
/
passless-auth
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
#!/bin/bash
debug=false
user=$USER
key_path=~/.ssh/id_ecdsa.pub
function usage()
{
cat << _EOF_
usage:
$(basename $0) -u [USER] -h [HOST IP] -p [SSH KEY PATH]
arguments:
-u Login User
-h Host IP Address
-p SSH Key File Path
_EOF_
exit
}
function add_key()
{
remote=$user@$host
cat $key_path | ssh -o StrictHostKeyChecking=no $remote "
mkdir -p ~/.ssh
chmod 700 ~/.ssh
cat >> ~/.ssh/authorized_keys
sort -u ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.bak
mv ~/.ssh/authorized_keys.bak ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
"
ssh -n -o PasswordAuthentication=no $remote true
}
while getopts ":u:h:p:" opt;
do
case $opt in
u)
user=${OPTARG}
;;
h)
host=${OPTARG}
;;
p)
key_path=${OPTARG}
;;
*)
break
;;
esac
done
if [ $OPTIND -eq 0 ] || [ -z "$user" ] || [ -z "$host" ] || [ -z "$key_path" ]
then
usage
fi
add_key