Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Loop Iteration

Reply
Thread Tools

Loop Iteration

 
 
RaH
Guest
Posts: n/a
 
      12-21-2005
Hello.

I am writing a script to parse my logfiles to monitor sshd attempted
logins. I know I'm reinventing the wheel, but it's something I want
to do. The problem I am having is that upon the 4th or 5th pass in my
for statement I recieve an error
AttributeError: 'NoneType' object has no
attribute 'group'When I tested against a smaller
version of my logs that contained roughly 8 ip's (one ip appears 6
times another 2) it works fine without the AttributeError.

My question is if it is in my implementation of RE or could it be a
memory issue?

This is a sample of what the log looks like [code:1:eaa2962442]
Dec 25 11:30:17 linux sshd[2198]: Received
disconnect from ::ffff:200.91.12.4: 11: Bye Bye
Dec 25 11:30:18 linux sshd[2199]:
input_userauth_request: illegal user sibylla
Dec 25 11:30:18 linux sshd[2199]: Could not
reverse map address 200.91.12.4.
Dec 25 11:30:18 linux sshd[2199]: Failed password
for illegal user sibylla from ::ffff:200.91.12.4 port
55697 ssh2[/code:1:eaa2962442]

Actual script[code:1:eaa2962442]
import re, string

def main():
match = 'Failed password for illegal user'
pattern = re.compile(match)
f = open('xaf', 'r')

instance = 0
for line in f:
if pattern.findall(line):
this = re.sub(
r'^([a-zA-Z]+)\s*([0-9]+)\s*([0-9]+)[0-9]+)[0-9]+)\s*([a-z]+)\s*([a-z]+)\s*([^0-9]+)\s*([0-9]+)\s*([^0-9]+)',
'', line, 1)

ip =
re.match(r'^(?P<ip>([0-9]+).([0-9]+).([0-9]+).([0-9]))',
this)

of = open("out.txt", 'a')
print ip.group('ip')
instance = instance + 1
of.close()
f.close()

if instance != 0:
print "%s match(s) found for Failed password for
illegal user" % instance


if __name__ == "__main__":
main()
[/code:1:eaa2962442]

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-21-2005
On Wed, 21 Dec 2005 18:05:22 +0000, RaH wrote:

> Hello.
>
> I am writing a script to parse my logfiles to monitor sshd attempted
> logins. I know I'm reinventing the wheel, but it's something I want
> to do. The problem I am having is that upon the 4th or 5th pass in my
> for statement I recieve an error
> AttributeError: 'NoneType' object has no
> attribute 'group'


At an interactive prompt, type help(re.match) and you will get:


match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.


When there is no match, your loop tries to call None.group. None has no
group attribute, just like the error message says.


--
Steven.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Try to exit inner loop and start next iteration of outter loop. Nene Perl Misc 6 12-13-2008 12:20 AM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Showing value of loop iteration in assert statement dwerdna VHDL 5 03-31-2005 05:23 PM
open new file each loop iteration Danny Anderson C++ 0 01-21-2004 08:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57