Friday , 21 October 2016

It's new!!!

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

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”.
That’s it!

Also read:

1, Enabling CSF managing privilege for WHM resellers
2, Install and configure csf on CentOS
3, 10 APF (Advance Policy Firewall) commands in Linux – Simple usages

About Arunlal Ashok

Linux Server Administrator. I'm dealing with Linux servers since 2012. I started this blog to share and discuss my ideas with the world. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *