COME GENERARE IL FILE .CSV
HOW TO GENERATE THE .CSV FILE
Se avete installato e funzionante Fail2Ban su Linux
If you have Fail2Ban installed and running on Linux
Se non l'avete installato fate riferimento alla pagina ufficiale del progetto:
If you haven't installed it, please refer to the project's official page:
https://github.com/fail2ban/fail2ban
Per generare la prima volta il file .csv
To generate the .csv file for the first time
ATTENZIONE: esporta TUTTI i Ban, dal giorno del primo avvio di Fail2ban, si consiglia di usarlo solo la prima volta:
ATTENTION: export ALL Bans, from the day of the first start of Fail2ban, it is recommended to use it only the first time:
Create il File con il vostro editor preferito (io uso vi)
Create the file with your favorite editor (I use vi)
# vi namefile.sh
copiate questo codice (adattate i percorsi alle vostre esigenze):
Copy this code (adapt the paths to your needs):
#!/bin/bash
# --- PATH CONFIGURATION ---
# --- CONFIGURAZIONE PERCORSI ---
TARGET_DIR="/your/folder/path/" ### change it ##### cambialo #####
TIMESTAMP=$(date +%Y%m%d_%H%M)
OUTPUT_FILE="${TARGET_DIR}namefile_${TIMESTAMP}.csv"
# 1. Active Jail Recovery (Improved Separator Cleaning)
# 1. Recupero Jail attive (pulizia migliorata dei separatori)
JAILS=$(sudo fail2ban-client status | grep "Jail list:" | sed 's/.*Jail list:\s*//;s/,//g')
# 2. Empty/Create the file at the beginning (optional)
# 2. Svuotiamo/Creiamo il file all'inizio (opzionale)
> "$OUTPUT_FILE"
# 3. Cycle through each Jail
# 3. Ciclo attraverso ogni Jail
for jail in $JAILS; do
## Let's clean up the output to make sure we only have clean lines: IP DATE TIME
# Puliamo l'output per assicurarci di avere solo righe pulite: IP DATA ORA
## We use 'tr -d' to remove any fail2ban formatting characters
# Usiamo 'tr -d' per rimuovere eventuali caratteri di formattazione di fail2ban
sudo fail2ban-client get "$jail" banip --with-time 2>/dev/null | grep -v "Lines" | while read -r line; do
## We extract the variables from the line
# Estraiamo le variabili dalla riga
ip=$(echo "$line" | awk '{print $1}')
bdate=$(echo "$line" | awk '{print $2}')
if [ -n "$ip" ] && [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]]; then
# Salviamo in APPEND (>>)
echo "$ip|$bdate|$jail" >> "$OUTPUT_FILE"
fi
done
done
echo "Report generato: $OUTPUT_FILE"
Rendete il file eseguibile:
Make the file executable:
# chmod +x namefile.sh
Ora è tutto pronto, potete lanciare lo script con questo comando e quando avrà finito avrete il vostro file.csv pronto per essere usato e uploadato su click2ban:
Now everything is ready, you can run the script with this command and once it's finished you will have your .csv file ready to be used and uploaded to click2ban:
# ./namefile.sh
Per aggiornare successivamente, dovrete fare un altro file eseguibile:
To update later, you will need to make another executable file:
Per aggiornare successivamente, dovrete fare un altro file eseguibile:
To update later, you will need to make another executable file:
I primi passi sono gli stessi, dovrete solo cambiare il contenuto del file.sh
The first steps are the same, you will just have to change the contents of the .sh file
copiate ed incollate il codice seguente ed adattatelo alle vostre esigenze:
Copy and paste the following code and adapt it to your needs:
#!/bin/bash
# --- PATH CONFIGURATION ---
# --- CONFIGURAZIONE PERCORSI ---
TARGET_DIR="/var/www/procedure/"
TIMESTAMP=$(date +%Y%m%d_%H%M)
OUTPUT_FILE="${TARGET_DIR}namefile_${TIMESTAMP}.csv"
# --- TIME FILTER CONFIGURATION ---
# --- CONFIGURAZIONE FILTRO TEMPO ---
# Change the number below to change the interval (e.g. 24 for a day, 168 for a week)
# Cambia il numero qui sotto per modificare l'intervallo (es. 24 per un giorno, 168 per una settimana)
TIME_LIMIT_HOURS=2
# Let's convert hours to seconds for comparison
# Convertiamo le ore in secondi per il confronto
TIME_LIMIT_SECONDS=$(( TIME_LIMIT_HOURS * 3600 ))
CURRENT_TIME=$(date +%s)
# 1. Active Jail Recovery (Improved Separator Cleaning)
# 1. Recupero Jail attive
JAILS=$(sudo fail2ban-client status | grep "Jail list:" | sed 's/.*Jail list:\s*//;s/,//g')
# 2. Empty/Create the file at the beginning (optional)
# 2. Inizializziamo il file (opzionale: aggiungi intestazione)
# echo "IP|DATA|JAIL" > "$OUTPUT_FILE"
> "$OUTPUT_FILE"
# 3. Cycle through each Jail
# 3. Ciclo attraverso ogni Jail
for jail in $JAILS; do
# Extract IP and Date/Time of the ban
# Estraiamo IP e Data/Ora del ban
sudo fail2ban-client get "$jail" banip --with-time 2>/dev/null | grep -v "Lines" | while read -r line; do
# Let's extract the data (fail2ban format: IP YYYY-MM-DD HH:MM:SS)
# Estraiamo i dati (formato fail2ban: IP YYYY-MM-DD HH:MM:SS)
ip=$(echo "$line" | awk '{print $1}')
bdate=$(echo "$line" | awk '{print $2}')
btime=$(echo "$line" | awk '{print $3}')
if [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]]; then
# Convert the ban date to seconds (timestamp)
# Convertiamo la data del ban in secondi (timestamp)
BAN_TIMESTAMP=$(date -d "$bdate $btime" +%s 2>/dev/null)
# Let's calculate the difference between current time and ban time
# Calcoliamo la differenza tra ora attuale e ora del ban
DIFF=$(( CURRENT_TIME - BAN_TIMESTAMP ))
# If the difference is less than the set limit, we save
# Se la differenza è minore del limite impostato, salviamo
if [ "$DIFF" -le "$TIME_LIMIT_SECONDS" ]; then
echo "$ip|$bdate $btime|$jail" >> "$OUTPUT_FILE"
fi
fi
done
done
echo "Report generato con i ban delle ultime $TIME_LIMIT_HOURS ore: $OUTPUT_FILE"
rendete eseguibile il file.sh e lanciatelo come spiegato nella procedura per il primo file.
make the .sh file executable and launch it as explained in the procedure for the first file.
adesso il file è pronto per essere eseguito da cron secondo i vostri tempi
Now the file is ready to be run by cron at your own pace.
ricordati di salvarlo ed uploadarlo su click2ban!
remember to save it and upload it to click2ban!