-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwless.sh
executable file
·55 lines (45 loc) · 1.6 KB
/
pwless.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
#!/bin/bash
#change this accoirding to your need.
KEYPATH="/home/keypath_of_server"
USER=ubuntu
# Get host list
count=0;
OUTDIR="out"
DIR=".pwlssh"
# Cleanup
rm -rf $OUTDIR/*
while read -r host ip
do
if [[ $host =~ '#' ]];
then
continue;
fi
hostlist[$count]=$host;
iplist[$count]=$ip;
# echo ${hostlist[$count]}
count=$(( $count + 1 ));
done < ./hostlist
if [ ! -d "$OUTDIR/$DIR" ]; then
mkdir -p $OUTDIR/$DIR;
fi
# Generate keys and copy them back
for ((i=0;i<${#hostlist[@]};i++)); do
scp -oStrictHostKeyChecking=no -i $KEYPATH ./sshkey.sh ubuntu@${hostlist[$i]}:/tmp
scp -oStrictHostKeyChecking=no -i $KEYPATH ./hostlist ubuntu@${hostlist[$i]}:/tmp
ssh -oStrictHostKeyChecking=no -i $KEYPATH ubuntu@${hostlist[$i]} "sudo su - $USER -c 'bash /tmp/sshkey.sh' "
scp -oStrictHostKeyChecking=no -i $KEYPATH ubuntu@${hostlist[$i]}:/tmp/id_rsa.pub $OUTDIR/$DIR/${hostlist[$i]}.pub
scp -oStrictHostKeyChecking=no -i $KEYPATH ubuntu@${hostlist[$i]}:~/.ssh/authorized_keys $OUTDIR/$DIR/${hostlist[$i]}.authorized_keys
ssh -oStrictHostKeyChecking=no -i $KEYPATH ubuntu@${hostlist[$i]} "sudo su - $USER -c 'rm /tmp/id_rsa.pub' "
done
for i in $OUTDIR/$DIR/*authorized_keys; do
cat $i >> $OUTDIR/authorized_keys
done
# Copy in authorized_keys
for i in $OUTDIR/$DIR/*.pub; do
cat $i >> $OUTDIR/authorized_keys
done
for ((i=0;i<${#hostlist[@]};i++)); do
scp -oStrictHostKeyChecking=no -i $KEYPATH $OUTDIR/authorized_keys ubuntu@${hostlist[$i]}:/tmp
ssh -oStrictHostKeyChecking=no -i $KEYPATH ubuntu@${hostlist[$i]} "sudo su - $USER -c 'cp /tmp/authorized_keys ~/.ssh/; ' "
done
rm -rf $OUTDIR