Skip to content

Instantly share code, notes, and snippets.

@tpokorra
Created November 29, 2022 14:46
Show Gist options
  • Save tpokorra/9e4fa382ef6c2ef0a7fc4f1198255ebb to your computer and use it in GitHub Desktop.
Save tpokorra/9e4fa382ef6c2ef0a7fc4f1198255ebb to your computer and use it in GitHub Desktop.
update_blocked_ips.sh
#!/bin/bash
# run in a cronjob, once a week
# see conditions at https://www.ipdeny.com/usagelimits.php
ipdeny_url="https://www.ipdeny.com/"
# see country codes at https://www.ipdeny.com/ipblocks/
# use space to separate multiple country codes
country_codes="cn"
domain_path="$HOME/doms/testsite.beispielverein.de"
function download_file() {
url=$1
filename=$2
# do we already have a file modified more than 5 days ago? then don't download it again
if [[ ! -f "$filename" || `find "$filename" -mtime +5` ]]; then
echo "downloading latest version from $url"
wget $url -O $filename
fi
}
function download_country() {
countrycode=$1
download_file "${ipdeny_url}/ipv6/ipaddresses/aggregated/${countrycode}-aggregated.zone" "$HOME/var/ipv6_${countrycode}.zone"
download_file "${ipdeny_url}/ipblocks/data/aggregated/${countrycode}-aggregated.zone" "$HOME/var/ipv4_${countrycode}.zone"
}
mkdir -p $HOME/var
for country_code in $country_codes; do
download_country $country_code
done
if [[ ! -f "$domain_path/.htaccess" && ! -f "$domain_path/.htaccess_template" ]]; then
echo "order allow,deny" > "$domain_path/.htaccess_template"
echo "allow from all" >> "$domain_path/.htaccess_template"
fi
if [ ! -f "$domain_path/.htaccess_template" ]; then
echo "missing $domain_path/.htaccess_template; not touching $domain_path/.htaccess"
exit -1
fi
cp -f $domain_path/.htaccess_template $domain_path/.htaccess
files="$HOME/var/*.zone"
for f in $files; do
while read p; do
echo "deny from $p" >> $domain_path/.htaccess
done <$f
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment