Python - 从系统日志文件中检索信息

发布于 2024-08-26 02:52:11 字数 138 浏览 7 评论 0原文

我被要求使用 python 编写一个程序来完成作业。

我收到了一个系统日志文件,我必须找出有关它的信息。

我如何找出登录 root 帐户的尝试次数?

任何建议将不胜感激,因为我对 python 很陌生,完全迷失了!

I have been asked to write a program using python for an assignment.

I have been given a syslog file and I have to find things out about it

How do I find out how many attempts were made to login to the root account?

Any advice would be highly appreciated as I am very new to python and completely lost!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉 2024-09-02 02:52:11

您需要 /var/log/auth.log,而不是 syslog。

它将包含这样的行:

Mar 20 10:47:24 Opus su[15918]: pam_unix(su:auth): authentication failure; logname=lfaraone uid=1000 euid=0 tty=/dev/pts/25 ruser=lfaraone rhost=  user=root

完成该问题的基本、简单的代码如下:

loginattempts = {"root": 0,
                 "someuser": 0,} # Usernames you want to check
with open('/var/log/auth.log', 'r') as authlog:
    for line in authlog:
        if "authentication failure" in line:
            username = line.split('=')[-1] # split the string into an array, 
                                           # using '=' as the delimiter
            if username in loginattempts: # is the username one we care about?
                loginattempts[username] += 1

就像用户 calmh 建议的那样,从长远来看,使用正则表达式进行解析可能会更好,但如果您还不知道它们,学习起来可能并不简单。

You want /var/log/auth.log, not syslog.

It'll contain lines like like this:

Mar 20 10:47:24 Opus su[15918]: pam_unix(su:auth): authentication failure; logname=lfaraone uid=1000 euid=0 tty=/dev/pts/25 ruser=lfaraone rhost=  user=root

Basic, naive code to accomplish the problem would be as follows:

loginattempts = {"root": 0,
                 "someuser": 0,} # Usernames you want to check
with open('/var/log/auth.log', 'r') as authlog:
    for line in authlog:
        if "authentication failure" in line:
            username = line.split('=')[-1] # split the string into an array, 
                                           # using '=' as the delimiter
            if username in loginattempts: # is the username one we care about?
                loginattempts[username] += 1

Like user calmh suggested, it will probably be better long-term to parse with regular expressions, but if you don't know them already, it can be non-trivial to learn.

冧九 2024-09-02 02:52:11

像这样的东西

#open the file , can be /var/log/messages, /var/log/maillog etc as defined in your system
f=open("mysyslogfile")
count=0 
#go through the file
for line in f:
   if "<unique pattern for checking root account login>" in line:
       count+=1
#close the file
f.close()
print "total count: " ,count

something like this

#open the file , can be /var/log/messages, /var/log/maillog etc as defined in your system
f=open("mysyslogfile")
count=0 
#go through the file
for line in f:
   if "<unique pattern for checking root account login>" in line:
       count+=1
#close the file
f.close()
print "total count: " ,count
昔日梦未散 2024-09-02 02:52:11

您可能需要读取该文件,解析每一行。当您找到与您感兴趣的内容相匹配的行(例如,根登录失败)时,您将增加一个计数器。

看看如何读取文件以及可能的如何使用正则表达式

如果您要对“实时”日志文件进行此检查(例如每五分钟一次),则需要跟踪已处理的文件量,以免每次都读取全部内容。这稍微复杂一些,因为您需要记住执行之间的状态(文件大小)。在这种情况下,请查看 shelve 模块。

You probably need to read the file, parsing each line. When you find a line that matches what you're interested in (failed root login, for example), you increment a counter.

Take a look at how to read files and possibly how to use regular expressions.

If you are going to do this check against a "live" log file, say every five minutes, you need to keep track of how much of the file you have already processed so you don't read it all every time. This is slightly more complicated, because you need to remember state (file size) between executions. In that case, look at the shelve module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文