Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; 'output': 0.05; 'subject:file': 0.07; 'input,': 0.09; 'output,': 0.09; 'skip:o 50': 0.09; 'todo:': 0.09; 'better?': 0.16; 'blocks': 0.16; 'chris,': 0.16; 'flush': 0.16; 'subject:txt': 0.16; 'timestamp': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'command': 0.22; 'input': 0.22; 'import': 0.22; 'rules': 0.22; 'header:User- Agent:1': 0.23; 'skip:l 30': 0.24; 'header:In-Reply-To:1': 0.27; "skip:' 10": 0.31; 'file': 0.32; 'skip:# 10': 0.33; 'version': 0.36; 'skip:o 20': 0.38; 'subject:new': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'read': 0.60; 'skip:t 30': 0.61; 'protection': 0.63; 'received:74.208': 0.68; "'with'": 0.84; 'received:74.208.4.194': 0.84; 'stamp': 0.91; 'subject:add': 0.91 Date: Fri, 24 May 2013 15:29:27 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Read txt file, add to iptables not working on new host References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:ti3ZAWLszXtp1zIynzwqYtJ8clJTkzebAJM9/bgckUe cNlbJ5JBuTGywPhXuQEBxDF7xuBeaq5Y2Rk5MuCIB0F5YGrbjF 104s7pS4/cpoPVgSMk+NngwBqtqzAKChsn5dd3URskE+D/blVn XKa2G/Mhv2l66twg28IlNqnDO+YMF7WqVkTt6HckNoP7Y9v8R8 YAGiSQKp0ODr0WyaujgcM9bdYrneyF7rkXhoQofrPvINvgaCKv sAhsWxVlTLPtXZ6QWNTTfyc9CD5mQd0TFRqVehLGFr1IY8+aLJ 3P3ttg0CtZ6Yl8VOasZi4NHcQuXbfEJKGG790E6PxUbub2wlx1 mrL5ZT+hhDpOuG+6ZM78= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369423780 news.xs4all.nl 15973 [2001:888:2000:d::a6]:55610 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45913 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