LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Iptables - Ban a list of ips trough a txt... (https://www.linuxquestions.org/questions/linux-newbie-8/iptables-ban-a-list-of-ips-trough-a-txt-706305/)

dan83 02-20-2009 05:00 PM

Iptables - Ban a list of ips trough a txt...
 
Is it possible to make iptables working to ban a list of ips charged from a txt file?
im using linux centos.

frieza 02-20-2009 05:08 PM

that would be called 'blacklisting'
here is some documentation i found as a start, try googling for iptables blacklisting if you need more
http://aplawrence.com/Words2005/2005_05_01.html

win32sux 02-20-2009 05:32 PM

Assuming your TXT file has one IP address per line (and nothing else):
Code:

#!/bin/sh
for i in `cat /etc/example.txt`; do
iptables -I INPUT -s $i -j DROP
done


dan83 02-21-2009 05:12 AM

Quote:

Originally Posted by win32sux (Post 3451715)
Assuming your TXT file has one IP address per line (and nothing else):
Code:

#!/bin/sh
for i in `cat /etc/example.txt`; do
iptables -I INPUT -s $i -j DROP
done


where i must insert this code? which file?

win32sux 02-21-2009 08:38 AM

Quote:

Originally Posted by dan83 (Post 3452102)
where i must insert this code? which file?

You don't really need to insert it anywhere. You could execute that script as a file of it's own an you'd be fine. But if you wanna insert it into your current iptables script then that's okay too. Anywhere in the script will work, as long as there are no -I rules after this which might conflict.

dan83 02-21-2009 10:24 AM

i have not understood... must i create a file with that script? how i can create it? and how execute it?

win32sux 02-21-2009 10:43 AM

Quote:

Originally Posted by dan83 (Post 3452315)
i have not understood... must i create a file with that script? how i can create it? and how execute it?

Yes, you could create a file for it if you want. You'd just need to edit the /etc/example.txt part in it to match whatever file you actually have the IPs stored in. You create it just like you would any other text file: Select the code I posted, right-click on it, select "Copy", open your favorite text editor, start a new file, right-click in it, select "Paste", edit the path/filename I just told you about, select "Save", type in the file name and path you wish to save the script as.

To make the file executable you right-click on the file, select "Properties" (or whatever), and tweak the permissions accordingly. You could also just use the chmod command on it like:
Code:

chmod 755 /etc/example.sh
To execute the file you just do a:
Code:

/etc/example.sh
BTW, I'm moving this thread to Newbie for better exposure.

dan83 02-21-2009 10:54 AM

must i save it as txt? then load it in the server?

win32sux 02-21-2009 11:02 AM

Quote:

Originally Posted by dan83 (Post 3452335)
must i save it as txt? then load it in the server?

No, you can call your files whatever you want.

My examples use TXT for the IP list file and SH for the script only for clarity's sake.

dan83 02-21-2009 11:32 AM

ok i do as you said and i tested it, but it does not work.
here the script i used:

Code:

#!/bin/sh
for i in `cat /banned_ips.txt`; do
iptables -I INPUT -s $i -j DROP
done

the file is in the main server directory as you can see... what is wrong? the banned ip can still connect.

frieza 02-21-2009 11:45 AM

it would help if you included the error message if any

dan83 02-21-2009 12:03 PM

it is an iptable txt ban;)do you think an error message should came out?;) and from where from the hat?

frieza 02-21-2009 12:08 PM

depends on what is broken, you could get a 'file not found error' if you input the wrong filename or a syntax error for iptables if the syntax were incorrect
the question is how do you know it isnt working?

dan83 02-21-2009 12:13 PM

because i inserted inside some ips of my friends, then i asked them to try to connect to the website and they can still connect;)

frieza 02-21-2009 12:49 PM

see if this helps ;)
http://www.developertutorials.com/tu...503/page1.html


All times are GMT -5. The time now is 07:29 PM.