-
Notifications
You must be signed in to change notification settings - Fork 3
/
keygen_master.sh
executable file
·84 lines (76 loc) · 2.11 KB
/
keygen_master.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
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
#!/bin/sh
this="$0"
while [ -h "$this" ]; do
ls=`ls -ld "$this"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '.*/.*' > /dev/null; then
this="$link"
else
this=`dirname "$this"`/"$link"
fi
done
# init base path
base=`dirname "$this"`
script=`basename "$this"`
base=`cd "$base"; pwd`
this="$base/$script"
slavesh="keygen_slave.sh"
slavescript="$base/$slavesh"
slaves="$base/slaves.conf"
hosts="$base/hosts.conf"
if [ ! -s ~/.ssh/id_rsa ]; then
expect -c "
spawn ssh-keygen -t rsa
expect {
\"*y/n*\" {send \"y\r\"; exp_continue}
\"*key*\" {send \"\r\"; exp_continue}
\"*passphrase*\" {send \"\r\"; exp_continue}
\"*again*\" {send \"\r\";}
}
"
fi
ssh-add $HOME/.ssh/id_rsa # Add private key
# batch ssh
if [ -s $hosts ]; then
for p in $(cat $hosts) #
do
username=$(echo "$p"|cut -f1 -d":") # Get username
ip=$(echo "$p"|cut -f2 -d":") # Get ip
password=$(echo "$p"|cut -f3 -d":") # Get password
id=$HOME/.ssh/id_rsa.pub
echo "ssh-copy-id -i $id $username@$ip -P $password"
# ssh-copy-id
expect -c "
spawn ssh-copy-id -i $id $username@$ip
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
}
"
done
fi
# dispath
if [ -s $slaves ]; then
for p in $(cat $slaves) #
do
username=$(echo "$p"|cut -f1 -d":") # Get username
ip=$(echo "$p"|cut -f2 -d":") # Get ip
password=$(echo "$p"|cut -f3 -d":") # Get password
id=$HOME/.ssh/id_rsa.pub
#ssh $username@$ip 'sudo apt-get install openssh*'
echo "scp $slavescript $hosts $username@$ip:~/ -P $password"
# Dispath to clients
expect -c "
spawn scp $slavescript $hosts $username@$ip:~/
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
}
"
# ssh to clients
echo "ssh $username@$ip 'sh $HOME/keygen_slave.sh'"
ssh $username@$ip 'sh $HOME/keygen_slave.sh'
done
fi