Read txt file, add to iptables not working on new host

Dave Angel davea at davea.name
Fri May 24 15:29:27 EDT 2013


On 05/24/2013 12:32 PM, JackM wrote:
> So Chris, does this version look better? Changed to inFile to with.
>
>
> #!/usr/bin/python
> import os
> import time
>
> # Input, Output, and TimeStamp
> logFile = open('/var/www/html/statistics/logs/banList.log','w')
> stamp = time.asctime(time.localtime())
>
> # Daily Flush of blockList rules before re-applying Blocks
> os.popen('/sbin/iptables -F INPUT')
> logFile.write(stamp+'\nFlushing Rules..\n')
>
> # Loop to read in file and Apply rules to IPtables
> with open('/var/www/html/mydomain.com/banlist.txt','r') as inFile:
>      for line in inFile:  # TODO: Use 'with' for a bit of protection
>          ip = line.split(';')[0]

You want to write the command to the logfile here, BEFORE you try the 
popen().  That way if there's a problem, you can see what it was about 
to try before it crashed.

>          output = os.popen( '/sbin/iptables -A INPUT -s ' + ip + ' -j
> REJECT' )
>          logFile.write(ip+' - Has been blocked\n')
>
>
>
>

-- 
DaveA



More information about the Python-list mailing list