-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiplist-add.sh
executable file
·58 lines (51 loc) · 1.54 KB
/
iplist-add.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
#!/bin/bash
execpath=/etc/iplist
conffile=autoadd.conf
[ -z "$(command -v ipset)" ] && echo "ipset not installed or not in \$PATH." && exit
while [ -n "$1" ]; do
case $1 in
-c)
[ -z "$(command -v dirname)" ] && echo "dirname not installed or not in \$PATH." && exit
[ -z "$(command -v basename)" ] && echo "basename not installed or not in \$PATH." && exit
shift
execpath="$(dirname $1)"
conffile="$(basename $1)"
shift
;;
*)
echo "\
usage: iplist-addipset.sh [options]
options: -c [config file location]
default location at /etc/iplist/autoadd.conf"
exit
;;
esac
shift
done
[ ! -f $execpath/$conffile ] && echo "$execpath/$conffile does not exist." && exit
function checkFlush() {
local ari=0
for setName in $flushedSets; do
[ "$setName" == "$1" ] && ari=1 && break
done
if [ "$ari" != "1" ]; then
ipset flush $1-4
ipset flush $1-6
flushedSets="$flushedSets $1"
fi
}
while read iplistFile iplistName iplistType; do
[ -z "$iplistFile" ] && continue
[ -z "$iplistType" ] && iplistType="net"
ipset -! create $iplistName-4 hash:$iplistType family inet
ipset -! create $iplistName-6 hash:$iplistType family inet6
checkFlush $iplistName
IPvX=4
echo -n "adding $iplistFile ..."
while read; do
[ -z "$REPLY" ] || ipset -! add $iplistName-$IPvX $REPLY 2> /dev/null && continue
grep : <<< $REPLY > /dev/null && IPvX=6 || IPvX=4
ipset -! add $iplistName-$IPvX $REPLY 2> /dev/null || echo "$REPLY cannot be add"
done < $execpath/db/$iplistFile
echo " done."
done < $execpath/$conffile