Skip to content
TrySitePrice

iptables: Read a List of IP Address From File And Block

Best/Tutorial

You can use iptables to block incoming traffic from a list of IP addresses stored in a file. Here's an example of how to do this in a bash script:

#!/bin/bash

# Define the path to the file containing the list of IP addresses

file="/path/to/ip.list"
# Loop through each line of the file

while read -r ip; do

 # Block incoming traffic from the IP address

 iptables -A INPUT -s "$ip" -j DROP

done < "$file"

# Save the iptables configuration

iptables-save > /etc/iptables.rules

This script reads each line of the file located at /path/to/ip.list and blocks incoming traffic from the IP addresses specified in the file. The iptables-save command at the end of the script saves the current iptables configuration so that it will persist after a reboot.