„update_blocklist.sh“ ändern
This commit is contained in:
parent
daa02a4f1c
commit
9da31d7a71
1 changed files with 25 additions and 13 deletions
|
@ -1,10 +1,10 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# /volume1/DEV/update_blocklist.sh
|
# /volume1/homes/admin/script/update_blocklist.sh
|
||||||
# Script import IP's from blocklist.de into DSM Auto-Block
|
# Script import IP's from blocklist.de
|
||||||
# https://www.synology-forum.de/showthread.html?103687-Freigabe-Blockierliste-automatisch-updaten&p=837478&viewfull=1#post837478
|
# https://www.synology-forum.de/showthread.html?103687-Freigabe-Blockierliste-automatisch-updaten&p=837478&viewfull=1#post837478
|
||||||
# version 0.1 by Ruedi61, 15.11.2016 / DSM 6.0.3
|
# version 0.1 by Ruedi61, 15.11.2016 / DSM 6.0.3
|
||||||
# version 0.2 by AndiHeitzer, 18.09.2019 / DSM 6.2.1 > add further Vars for DB
|
# version 0.2 by AndiHeitzer, 18.09.2019 / DSM 6.2.1 > add further Vars for DB
|
||||||
# version 0.3 by geimist, 19.09.2019 / DSM 6.2.2 > enlarge Stats / Loglevel
|
# version 0.3 by geimist, 20.09.2019 / DSM 6.2.2 > add Stats / Loglevel / speed improvement
|
||||||
|
|
||||||
# TYPE=0 > Blacklist / TYPE=3 > Whitelist
|
# TYPE=0 > Blacklist / TYPE=3 > Whitelist
|
||||||
TYPE=0
|
TYPE=0
|
||||||
|
@ -21,26 +21,35 @@ DELETE_IP_AFTER="7"
|
||||||
LOGLEVEL=1
|
LOGLEVEL=1
|
||||||
|
|
||||||
###############################################################################################################
|
###############################################################################################################
|
||||||
# Do NOT change after here
|
# Do NOT change after here!
|
||||||
###############################################################################################################
|
|
||||||
|
|
||||||
|
# SQL Create-Statement:
|
||||||
# CREATE TABLE AutoBlockIP(IP varchar(50) PRIMARY KEY,RecordTime date NOT NULL,ExpireTime date NOT NULL,Deny boolean NOT NULL,IPStd varchr(50) NOT NULL,Type INTEGER,Meta varchar(256))
|
# CREATE TABLE AutoBlockIP(IP varchar(50) PRIMARY KEY,RecordTime date NOT NULL,ExpireTime date NOT NULL,Deny boolean NOT NULL,IPStd varchr(50) NOT NULL,Type INTEGER,Meta varchar(256))
|
||||||
if [ $(whoami) != "root" ]; then
|
if [ $(whoami) != "root" ]; then
|
||||||
echo "WARNING: this script must run from root!" >&2
|
echo "WARNING: this script must run from root!" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
countadded=0
|
countadded=0
|
||||||
countskipped=0
|
countskipped=0
|
||||||
UNIXTIME=$(date +%s)
|
UNIXTIME=$(date +%s)
|
||||||
UNIXTIME_DELETE_IP=$(date -d "+$DELETE_IP_AFTER days" +%s)
|
UNIXTIME_DELETE_IP=$(date -d "+$DELETE_IP_AFTER days" +%s)
|
||||||
wget -q "https://lists.blocklist.de/lists/$BLOCKLIST_TYP.txt" -O /tmp/blocklist.txt
|
# current IP-list:
|
||||||
IPcountList=$(cat "/tmp/blocklist.txt" | grep -Eo "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" | wc -l)
|
sqlite3 -header -csv /etc/synoautoblock.db "select IP FROM AutoBlockIP WHERE TYPE='0' ORDER BY 'IP' ASC;" | sed -e '1d' | sort > /tmp/before.txt
|
||||||
|
# load online IP-list:
|
||||||
|
curl -s "https://lists.blocklist.de/lists/${BLOCKLIST_TYP}.txt" | sort > /tmp/onlinelist.txt
|
||||||
|
# filter diffs:
|
||||||
|
diff "/tmp/before.txt" "/tmp/onlinelist.txt" | grep '^>' | sed -e 's/>//' > /tmp/blocklist.txt # only diffs from left to right
|
||||||
|
# stats …
|
||||||
|
IPcountdiffs=$(cat "/tmp/blocklist.txt" | grep -Eo "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" | wc -l)
|
||||||
|
IPcountList=$(cat "/tmp/onlinelist.txt" | grep -Eo "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" | wc -l)
|
||||||
|
|
||||||
while read BLOCKED_IP
|
while read BLOCKED_IP
|
||||||
do
|
do
|
||||||
# Check if IP valid
|
# Check if IP valid
|
||||||
VALID_IPv4=$(echo "$BLOCKED_IP" | grep -Eo "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" | wc -l)
|
VALID_IPv4=$(echo "$BLOCKED_IP" | grep -Eo "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" | wc -l)
|
||||||
|
|
||||||
|
echo "$BLOCKED_IP"
|
||||||
|
|
||||||
if [[ $VALID_IPv4 -eq 1 ]]; then
|
if [[ $VALID_IPv4 -eq 1 ]]; then
|
||||||
# Convert IPv4 to IPv6 :)
|
# Convert IPv4 to IPv6 :)
|
||||||
|
@ -52,21 +61,23 @@ while read BLOCKED_IP
|
||||||
countadded=$(( $countadded + 1 ))
|
countadded=$(( $countadded + 1 ))
|
||||||
if [[ $LOGLEVEL -eq 2 ]]; then
|
if [[ $LOGLEVEL -eq 2 ]]; then
|
||||||
echo "IP added to Database! --> $BLOCKED_IP"
|
echo "IP added to Database! --> $BLOCKED_IP"
|
||||||
elif [[ $LOGLEVEL -eq 1 ]]; then
|
# elif [[ $LOGLEVEL -eq 1 ]]; then
|
||||||
echo -n "."
|
# echo -n "."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
countskipped=$(( $countskipped + 1 ))
|
countskipped=$(( $countskipped + 1 ))
|
||||||
if [[ $LOGLEVEL -eq 2 ]]; then
|
if [[ $LOGLEVEL -eq 2 ]]; then
|
||||||
echo "IP already in Database! --> $BLOCKED_IP"
|
echo "IP already in Database! --> $BLOCKED_IP"
|
||||||
elif [[ $LOGLEVEL -eq 1 ]]; then
|
# elif [[ $LOGLEVEL -eq 1 ]]; then
|
||||||
echo -n "."
|
# echo -n "."
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done < /tmp/blocklist.txt
|
done < /tmp/blocklist.txt
|
||||||
|
|
||||||
rm /tmp/blocklist.txt
|
rm /tmp/blocklist.txt
|
||||||
|
rm /tmp/before.txt
|
||||||
|
rm /tmp/onlinelist.txt
|
||||||
|
|
||||||
if [[ $LOGLEVEL -eq 1 ]] || [[ $LOGLEVEL -eq 2 ]]; then
|
if [[ $LOGLEVEL -eq 1 ]] || [[ $LOGLEVEL -eq 2 ]]; then
|
||||||
END=$(date +%s)
|
END=$(date +%s)
|
||||||
|
@ -74,9 +85,10 @@ if [[ $LOGLEVEL -eq 1 ]] || [[ $LOGLEVEL -eq 2 ]]; then
|
||||||
echo -e
|
echo -e
|
||||||
echo "duration of the process: $RUNTIME Seconds"
|
echo "duration of the process: $RUNTIME Seconds"
|
||||||
echo "count of IPs in list: $IPcountList"
|
echo "count of IPs in list: $IPcountList"
|
||||||
|
echo "count of diffs: $IPcountdiffs"
|
||||||
echo "added IPs: $countadded"
|
echo "added IPs: $countadded"
|
||||||
echo "skipped IPs: $countskipped"
|
echo "skipped IPs: $countskipped"
|
||||||
echo "count of blocked IPs: $(sqlite3 /etc/synoautoblock.db "SELECT count(IP) FROM AutoBlockIP WHERE TYPE='0' " )"
|
echo "count of blocked IPs: $(sqlite3 /etc/synoautoblock.db "SELECT count(IP) FROM AutoBlockIP WHERE TYPE='0' " )"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Reference in a new issue