[英]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.
我尝试做的一件事是提取尝试失败次数超过 30 次的 IP 地址列表。 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如果我使用 python 创建一个计数器来查找关键字失败,我总计并打印出来
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.