Link to home
Start Free TrialLog in
Avatar of umaxim
umaximFlag for United States of America

asked on

hosting php

Hi i want to block 2500 ip address from database. But i do not want to enter each CIDR in iptables it will take me long time. How i can make some script which will connect to ssh and add CIDR to iptables. I use ubuntu linux. Or maybe it have some bash script to take information from database and block it.
Avatar of DataCruncher
DataCruncher
Flag of Canada image

If I understand well, you have a database in which there is a table that contains all the IP address you would like to block on your Ubuntu server using the iptables command?

If that's the case, one way you could generate quickly the list of commands you need to execute would be the following:

Considering that the syntax for iptables command on Ubuntu to block an IP address is (please verify on your system):
iptables -A INPUT -s <IP-ADDRESS> -j DROP

Then you could execute that SQL query on your database and copy & paste the result in a file on your Ubuntu to run it as a script:

SELECT 'iptables -A INPUT -s ' || MyTableFieldWithTheIpAddress || ' -j DROP' "Commands"
FROM MY_TABLE_WITH_THE_IP_ADDRESSES;

hi,

if you want shell script and linux thn why php?
or do u want to stop then from viewing your php website.
if that so, just
ALGO

check client address using php and check whether its present in your db if yes display them the reason that's it.
or else if you want to do it via linux than
@dtatcruncher method will do
Avatar of umaxim

ASKER

I just do not know shell language this why i try to use php to create it.
k,
as i had commented earlier if ur trying in php.
than get client ip and check in the database for this array.

this link will help you to get client ip.
http://www.phpbuilder.com/board/showthread.php?t=10327697
and then extract the block ip from database. and use in_array() to check whethetr present if yes than stop the display.
Avatar of umaxim

ASKER

No i want to use iptable becouse i will block it on 10 website on all server my client need it. So i need to put 3000 ip to database i just do not want to copy and past each of them i just want to combine them in one or something.
I guess this list of IP will need to be refresh on a periodic basis?  If that's the case, as you mentioned 10 websites/servers on which you want to apply this iptables IP blocks, you'll need a sync mechanism.

I'm no Linux guru so here is how the jack of all trades I am would do it:

1- on one of the server, run a script (language stays to determine) to extract that list of IP to block, from the database, and generate a script file that will be executed with iptables commands to block the IP, a script file in Linux is like a batch file in Windows/DOS, it's a serie of commands that would run at the console prompt;

2- then using a Linux CRON job (scheduler), you could ftp upload this script to the 9 or more other servers

3- another CRON job would be defined on all servers to run the script to update the iptables blocked IPs

If that scenario could apply to you, I can elaborate a little more.

By the way, what is the database these IP are in? (MySQL, Oracle, ...)
Avatar of umaxim

ASKER

ok i have mysql database where it have list of cidr

i need to run list of command on one server with command iptables -A INPUT -s {CIDR} -j DROP
so i need to generate some batch file which will be run as command list in shell of linux i can do it by my self i jest do not want to put each ip by the hand.
ASKER CERTIFIED SOLUTION
Avatar of DataCruncher
DataCruncher
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial