-
Notifications
You must be signed in to change notification settings - Fork 3
/
htlist.sh
executable file
·66 lines (57 loc) · 2.15 KB
/
htlist.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
# +----+----+----+----+
# | | | | |
# Author: Mark David Scott Cunningham | M | D | S | C |
# +----+----+----+----+
# Created: 2014-08-03
# Updated: 2014-08-04
#
#
#!/bin/bash
htlist(){
# Parse through options/parameters
if [[ $1 =~ -p ]]; then SITEPATH=$2; shift; shift; else SITEPATH='.'; fi; opt=$1
# Run correct for-loop given list type
case $opt in
-b | --black)
if ! grep -Eq '.rder .llow,.eny' $SITEPATH/.htaccess &> /dev/null; then
sudo -u $(getusr) echo -e "Order Allow,Deny\nAllow From All" >> $SITEPATH/.htaccess &&
echo "$SITEPATH/.htaccess rules updated";
fi
shift; echo; for x in "$@"; do
sed -i "s/\b\(.rder .llow,.eny\)/\1\nDeny From $x/" $SITEPATH/.htaccess &&
echo "Deny From $x ... Added to $SITEPATH/.htaccess"
done; echo
;;
-w | --white)
if ! grep -Eq '.rder .eny,.llow' $SITEPATH/.htaccess &> /dev/null; then
sudo -u $(getusr) echo -e "Order Deny,Allow\nDeny From All" >> $SITEPATH/.htaccess &&
echo "$SITEPATH/.htaccess rules updated";
fi
shift; echo; for x in "$@"; do
sed -i "s/\b\(.rder .eny,.llow\)/\1\nAllow From $x/" $SITEPATH/.htaccess &&
echo "Allow From $x ... Added to $SITEPATH/.htaccess"
done; echo
;;
-r | --robot)
if grep -Eq '.rder .llow,.eny' $SITEPATH/.htaccess &> /dev/null; then
sed -i 's/\b\(.rder .llow,.eny\)/\1\nDeny from env=bad_bot/' $SITEPATH/.htaccess
else sudo -u $(getusr) echo -e "\nOrder Allow,Deny\nDeny from env=bad_bot\nAllow from All" >> $SITEPATH/.htaccess && echo "$SITEPATH/.htaccess rules updated."; fi
shift; echo
echo -e "\n# ----- Block Bad Bots Section -----\n" >> $SITEPATH/.htaccess
for x in "$@"; do echo "BrowserMatchNoCase $x bad_bot" | tee -a $SITEPATH/.htaccess; done
echo -e "\n# ----- Block Bad Bots Section -----\n" >> $SITEPATH/.htaccess; echo
;;
-h|--help|*)
echo -e "\n Usage: htlist [options] <listType> <IP1> <IP2> ...\n
Options:
-p | --path .... Path to .htaccess file
-h | --help .... Print this help and quit
List Types:
-b | --black ... Blacklist IPs
-w | --white ... Whitelist IPs
-r | --robot ... Block UserAgent\n";
return 0;
;;
esac
}
htlist "$@"