Help to complete a script (fake google IPs, maybe fail2ban)

Solution 1:

You can try this approach.

For Apache, to Configure a webserver "jail" in fail2ban configuration, there is a guide: fail2ban with Apache

Here you can configure temporary bans for IPs and also there is a client fail2ban-client that you could call directly from your php script (need to check permissions) to ban IPs manually.

I think this will work better than the cron approach, but if you still want to go via shell:

#!/bin/bash
#assuming one IP per line
input="/var/www/html/function_global/ip_add_fwd.txt"
while IFS= read -r line
do
    sudo ufw deny from $line to any;
done < "$input"

#This line will empty the file
echo "">"$input"

script with fail2ban

#!/bin/bash
#assuming one IP per line
input="/var/www/html/function_global/ip_add_fwd.txt"
while IFS= read -r line
do
    #sudo ufw deny from $line to any;
    sudo fail2ban-client set apache-badbots banip $line;
done < "$input"

#This line will empty the file
echo "">"$input"  

#added by JP - Will list all banned IPs for apache-badbots
sudo fail2ban-client get apache-badbots banip --with-time;