-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrollr-drupal.sh
executable file
·204 lines (160 loc) · 4.71 KB
/
trollr-drupal.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
#
# Drupal site blocked IPs comparision and additions
#
# Written by: Ryan Johnson [ [email protected] ]
# Created: April 2021
# Updated: April 2021
#
# - takes a list of known bad actor IPs
# - pulls current list of IPs from Drupal table blocked_ips
# - compares the list and manually adds to the table via terminus drush
# - gathers 200 IP address then adds in a batch for performance
#
###
### files & variables ###
###
# file for the list of IPs that are known bad actors
fileTrolls=`cat trolls.dat`
# counter for how many got banned
counterBanned=0
# counter for the loop
counterLoop=0
# var to hold the 200 ips
hundoIPs=""
# var to start the sitename
SITENAME=""
###
### functions ###
###
# check if a user is logged into terminus
# script stops if user is not logged in
terminus_auth_check() {
# check if user is logged into terminus
response=`terminus auth:whoami`
# check the result
# not logged in so let the user know
if [ "$response" == "" ]; then
echo "you are not authenticated with terminus, please login with terminus auth:login and re-run the script"
exit 0
# user login found so make sure its correct user
else
echo "logged in as $response"
fi
}
###
### flag handles
###
while getopts ":s:" opt; do
case $opt in
s)
# got a flag so set to the variable
SITENAME=$OPTARG
;;
\?)
echo "unsupported flag: -$OPTARG" >&2
exit 1
;;
:)
echo "flag -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
###
### main ###
###
# check for logged in user
terminus_auth_check
# check if the sitename has already been set or not
if [ -z "${SITENAME}" ]; then
# grab the sites
echo "grabbing site list..."
terminus site:list --fields="name"
# set the site to use
read -p 'type in site name and press [Enter] to start trollr on: ' SITENAME
fi
# display status message while it gathers IPs from Drupal
printf "\n"
printf "grabbing list of blocked IPs from ${SITENAME} live environment... \n"
# grab the list of IPs already blocked from the site
fileDrupal=`terminus drush ${SITENAME}.live -- sql-query 'SELECT ip FROM blocked_ips' 2>/dev/null`
# display status message while it compares the drupal IPs to known bad actor IPs
printf "comparing list of blocked IPs to known bad actor list... \n\n"
# set the new IPs that will be added to the table
diffIPs=$(comm -23 <(tr ' ' '\n' <<<"$fileTrolls" | sort) <(tr ' ' '\n' <<<"$fileDrupal" | sort))
# get the number of IPs
numDiff=`echo -n "$diffIPs" | grep -c '^'`
# check if there are IPs to add
if [ "$numDiff" != 0 ]; then
# display and ask to continue
read -p "$numDiff number of new IPs to block, continue? [y/n] " yn
case $yn in
[Yy]* )
# before looping inform user
printf "\n"
printf "starting loop, this may take a while depending on the number of additions...\n"
# yes so loop thru list and add to table
for banIP in $diffIPs; do
# if the counter is 200
if [ "$counterLoop" == 199 ]; then
# add it to the counter
counter=$(( $counter + 1 ))
# have 200 so reset counter
counterLoop=0
# add in the IPs
hundoIPs="$hundoIPs , ('$banIP')"
# quick echo
printf "%s" "adding entries..."
# terminus call
# for adding single entries
# echo terminus drush ${SITENAME}.live -- sql-query 'INSERT IGNORE INTO blocked_ips SET ip = "'$hundoIPs'"' &>/dev/null
# for adding multiple entries
terminus drush ${SITENAME}.live -- sql-query "INSERT IGNORE INTO blocked_ips (ip) VALUES $hundoIPs" &>/dev/null
# status update
printf "success! $counter IPs added\n"
# reset the var to hold the 200 IPs
hundoIPs=""
# not to 200 IPs yet
else
# add it to the counter
counter=$(( $counter + 1 ))
# not 200 yet so bump counter
counterLoop=$(( $counterLoop + 1 ))
# check if there has been an IP added or not
if [ -z "${hundoIPs}" ]; then
# its blank so just set
hundoIPs="('$banIP')"
# it has an IP in it
else
# add to end of var
hundoIPs="$hundoIPs , ('$banIP')"
fi
fi
done
# need to check insert incase the counter wasnt reached
if (( "$counterLoop" < 199 )); then
# quick status message
printf "%s" "loop done, inserting the leftovers..."
# for adding multiple entries
terminus drush ${SITENAME}.live -- sql-query "INSERT IGNORE INTO blocked_ips (ip) VALUES $hundoIPs" &>/dev/null
# status update
printf "success!\n\n"
fi
# done with loop so let user konw
echo "$counter IPs were added to blocked_ips table"
echo "done and exiting..."
exit 0;;
[Nn]* )
# no so exit script
echo "exiting script..."
exit 0;;
esac
# no ips to add
else
# no so exit script
echo "no IPs to add, exiting script..."
exit 0
fi
# exit just in case
exit 0