-
Notifications
You must be signed in to change notification settings - Fork 5
/
keyputter.sh
executable file
·119 lines (101 loc) · 2.13 KB
/
keyputter.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
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
#!/bin/bash
authorized_keys="/etc/dropbear/authorized_keys"
user=root
sshOptionen="-q -oBatchMode=yes -oPasswordAuthentication=no -oConnectTimeout=10s -oStrictHostKeyChecking=no"
counter_keys_to_add=0
counter_keys_to_delete=0
counter_hosts=0
keys_to_add=()
keys_to_delete=()
hosts=()
function add_key () {
if [ ! -s "$1" ]
then
echo "WARNING: $i does not exist, skipping."
else
counter_keys_to_add=$(( $counter_keys_to_add + 1 ))
keys_to_add[$counter_keys_to_add]=$1
fi
}
function delete_key () {
if [ ! -s "$1" ]
then
echo "WARNING: $i does not exist, skipping."
else
counter_keys_to_delete=$(( $counter_keys_to_delete + 1 ))
keys_to_delete[$counter_keys_to_delete]=$1
fi
}
function add_host () {
counter_hosts=$(( $counter_hosts + 1 ))
hosts[$counter_hosts]=$1
}
function process_Arguments () {
while [ ! -z "$1" ]
do
case "$1" in
"-a")
add_key $2
shift
;;
"-d")
delete_key $2
shift
;;
*)
add_host $1
;;
esac
shift
done
}
function areThereEnoughArgumentsGiven () {
if [[ ( $counter_keys_to_add -gt 0 || $counter_keys_to_delete -gt 0 ) && $counter_hosts -gt 0 ]]
then
return 0
else
return 1
fi
}
function display_usage () {
echo 'This tool adds or removes public ssh keys from multiple hosts.'
echo
echo 'Usage: $0 [-a public_keyfile|-d public_keyfile]... host [host]...'
}
function compose_command_string () {
command='if [[ ! `tail -c 1 '$authorized_keys'` == "" ]];then echo >> '$authorized_keys';fi;'
for i in "${keys_to_add[@]}"
do
key=`cat $i`
command="$command""echo $key >> $authorized_keys;"
done
if [[ $counter_keys_to_delete -gt 0 ]]; then
command="$command"" sed "
for i in "${keys_to_delete[@]}"
do
key=`cat $i |cut -f2 -d" "`
command="$command"" -i '\#$key#d'"
done
command="$command"" $authorized_keys;"
fi
echo $command
}
function ssh_on_node {
ssh $sshOptionen $user@$1 $2
return $?
}
process_Arguments "$@"
if ! areThereEnoughArgumentsGiven
then
display_usage
exit 1
fi
command=$(compose_command_string)
for i in "${hosts[@]}"
do
ssh_on_node $i "$command"
if [ ! $? == '0' ]
then
echo "ERROR: $i fehlgeschlagen."
fi
done