Tuesday, June 11, 2013

PHP-SHELL script to whitelist IP on CSF

I needed a fast way to whitelist my IP on a linux server to be able to login to secure administration page...

I set it up by using a php file + shell file + cronjob

Now I easily open a public website address "mydomain.com/whatis.php" and it automatically whitelist my IP.

"whatis.php" PHP file code :
<?php
echo file_put_contents("/tmp/iplog",$_SERVER["REMOTE_ADDR"]);
?>


"/script/ip" Shell file code :
#!/bin/bash
i=1
while [ $i -le 10 ]
do
        status=`cat /tmp/iplog`
        if [ "$status" != "0" ]; then
                echo $status >> /etc/csf/csf.allow
                echo $status >> /etc/csf/csf.ignore
#                echo "allow from $status" >> /home/DOMAIN/public_html/admin/.htaccess
                echo "0" > /tmp/iplog
                `/etc/csf/csf.pl -r > /tmp/csf.log`
        fi
        sleep 5
        (( i++ ))
done

"/script/ip" Cronjob :
* * * * * /script/ip

Note : I tried to run `csf -r` to reset CSF firewall in shell script but it didn't work ! I don't know why. I found here that we should use `/etc/csf/csf.pl -r` instead. Thanks to that guy !!

No comments:

Post a Comment

How to export Apple Health / Google Fit training activity to TCX format

  I own a Xiaomi Smart Band 7, and recently, my Mi Fitness app stopped syncing running activities to Strava. Mi Fitness supports syncing dat...