简体   繁体   中英

Python counter to text file

So im trying to analyse a log file and extract information from it. One of the things im trying to do is extract a list of IP addresses that have more than 30 failed attempts. In this a failed attempt is one that starts with the line failed password for.

I have an idea for this that i wanted to try as i wasn't sure whether it will work.

If i use python to create a counter that looks for the keyword failed that i total and print out

This is what i have so far

failed_line=0
with open('blacklisttips.txt') as f2:
    lines= f1.readlines()
    for i, line in enumerate (lines):
        if line.startswith(failed_line):
            f2.write(line)
            f2.write(lines[i+1])

So let's say your file looks like this:

failed password for 192.168.1.1
failed password for 192.168.1.2
...
more similar lines


import collections

prefix = failed password for
with open('path/to/file') as infile:
    counts = collections.Counter(line.rsplit(" ",1)[1] for line in infile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM