-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssh-copy-id-auto.sh
executable file
·51 lines (40 loc) · 1.35 KB
/
ssh-copy-id-auto.sh
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
#!/bin/bash
#
# Author: Fazle Arefin
# Purpose: Copies your public key to servers for passwordless auth
#
# TODO: change this path to the file containing your password
# the password can be in plain-text or base64 encoded; don't add any blank line
# if the password is base64 encoded, change the value of $Base64_Enc
export Pwd_File='/home/fazle/.passwd/fazle'
# TODO: base64 encoding; 0 is off, 1 is on
export Base64_Enc=0
# TODO: number of hosts to copy your key in parallel
Parallel_Copy=10
Hosts=$@
# some basic checks
if [[ "$1" == "" ]]; then
echo "No host(s) provided" 1>&2
exit 44
elif [[ 0 -ne $(hash sshpass &> /dev/null)$? ]]; then
echo "Install sshpass first" 1>&2
exit 55
elif ! [[ -f $Pwd_File ]]; then
echo "Password file missing" 1>&2
exit 66
fi
copy_ssh_id() {
host=$(echo -n $1) # strip the newline character from $1
if [[ $Base64_Enc -eq 1 ]]; then
sshpass -p "$(base64 -d $Pwd_File)" ssh-copy-id -o ConnectTimeout=5 $host &> /dev/null
else
sshpass -p "$(cat $Pwd_File)" ssh-copy-id -o ConnectTimeout=5 $host &> /dev/null
fi
if [[ $? -eq 0 ]]; then
echo "✔ Copied ssh public key to $host" 1>&2
else
echo "✘ Could not copy ssh public key to $host" 1>&2
fi
}
export -f copy_ssh_id
echo ${Hosts[*]} | xargs -d' ' -n1 -P${Parallel_Copy} bash -c 'copy_ssh_id "$@"' _