我需要分析一个系统日志。我被告知我应该拆分一个列表,然后遍历它来查找一个IP地址。这是日志的一小部分。有重复的条目,因此我只能注意到包含单词“从根失效密码”的行。Python - 分割一个列表,然后循环查找一个IP地址
Jan 10 09:32:07 j4-be03 sshd[3876]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=218.241.173.35 user=root
Jan 10 09:32:09 j4-be03 sshd[3876]: Failed password for root from 218.241.173.35 port 47084 ssh2
Jan 10 09:32:17 j4-be03 sshd[3879]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=218.241.173.35 user=root
Jan 10 09:32:19 j4-be03 sshd[3879]: Failed password for root from 218.241.173.35 port 47901 ssh2
Jan 10 09:32:26 j4-be03 sshd[3881]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=218.241.173.35 user=root
Jan 10 09:32:29 j4-be03 sshd[3881]: Failed password for root from 218.241.173.35 port 48652 ssh2
这是我的代码到目前为止,还有一些伪代码。
f=open('auth','r')
count=0
for line in f:
if "failed password for root from" in line:
count +=1
if count>=13:
take the ip address, remove duplicates and print the address
如果有从一个IP地址13次或更多的尝试这个地址必须被添加到一个文件中。我知道如何写一个新文件,但是如果可能的话,一个小例子会很方便。我所熟悉的.append
你可以使用正则表达式? – 2012-03-11 00:19:17
不,我没有正则表达式的经验对不起 – bigl 2012-03-11 00:25:49