For loop to manage multiple IP addresses with CSF
Hey, I want to block a range of IP addresses in firewall!!
It’s simple to block or allow IP addresses into the firewall by using simple for loop via command line. In some cases like DoS, and we found some IP addresses which are accessing the website or server many times. We can create a simple for looped script to move all of them to the firewall. Here I’m illustrating this with CSF. Config Server Firewall is abbreviated as CSF. CSf is the most commonly using firewall application to secure Linux servers. CSF has wide range of options to manage Linux firewall via comman-line and from the control panel. The csf installation includes preconfigured configurations and control panel UI’s for cPanel, DirectAdmin and Webmin. The installation ans usage of CSF is quit simple.
To deny multiple IPs:
The command to deny an IP:
# csf -d ip.address
Click here for more CSF commands
Here is the script to block number of IPs:
1, Save the IPs in a text file:
vi block.txt ..... 126.96.36.199 188.8.131.52 184.108.40.206 220.127.116.11 .....
2, Blocking them using for loop:
# for i in `cat block.txt`; do csf -d $i;done
You can customize the script as per your requirement. If you want to add UIP address change the “do” part with “csf -a $i”.