-
Notifications
You must be signed in to change notification settings - Fork 10
/
ipunban.sh
28 lines (22 loc) · 682 Bytes
/
ipunban.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
#!/bin/bash
CSV_FILE="blocked_ips.csv"
function check_ip() {
local ip=$1
local current_time=$(date +%s)
local end_time=$(awk -F',' -v ip="$ip" '$1 == ip {print $2}' "$CSV_FILE")
if [ -n "$end_time" ]; then
local block_time_seconds=$((end_time - current_time))
if ((block_time_seconds <= 0)); then
if iptables -C INPUT -s $ip -j DROP &> /dev/null; then
iptables -D INPUT -s $ip -j DROP
echo "Unbanned IP: $ip"
fi
awk -F',' -v ip="$ip" '$1 != ip' "$CSV_FILE" > "$CSV_FILE.tmp" && mv "$CSV_FILE.tmp" "$CSV_FILE"
fi
fi
}
if [ -f "$CSV_FILE" ]; then
while IFS=',' read -r ip _; do
check_ip "$ip"
done < "$CSV_FILE"
fi