machineaddict
machineaddict

Reputation: 3236

Block ips with iptables from list with comments

I have the following ip blocking list:

123.151.149.222#China Telecom TIANJIN, CN
91.188.124.0/23#Hosting Company, PL
134.145.0.0/16#Shell Information Technology International, CN
134.146.0.0/15#Shell Information Technology International, CN

I want to block those ips with iptables like this:

BANNED_IPS=$(egrep -v -E "^#|^$" /etc/blocked.ips.list)

for ip in $BANNED_IPS
do
    iptables -A INPUT -s $ip -m comment --comment "$comment" -j DROP
done

Example: where $ip should be 123.151.149.222 and $comment should be China Telecom TIANJIN, CN. And, yes, I know the above script doesn't work. I would like the script to be like the one above.

Question: How can i achieve this?

Upvotes: 0

Views: 381

Answers (1)

Barmar
Barmar

Reputation: 782498

IFS='#'
egrep -v '^#|^$' /etc/blocked.ips.list |
    while read ip comment
    do
        $BANNED_IPS =A INPUT -s $ip -m comment --comment "$comment" -j DROP
    done

Explanation:

IFS='#'

changes the shell's field separator, used by read and word-splitting after variable expansion.

Upvotes: 1

Related Questions