【问题标题】:Python, Read a text file and print out a list of invalid user namesPython,读取文本文件并打印出无效用户名列表
【发布时间】:2019-11-08 00:40:43
【问题描述】:

我需要读取 linux /var/log/auth.log 文件并打印系统登录失败的用户。该文件的某些行将如下所示:

May 26 07:29:20 instance-1 sshd[20327]: Disconnected from 61.147.247.146 port 45177 [preauth]
May 26 07:32:22 instance-1 sshd[20351]: Invalid user nagios from 159.65.144.233 port 49715
May 26 07:32:22 instance-1 sshd[20351]: input_userauth_request: invalid user nagios [preauth]
May 26 07:32:23 instance-1 sshd[20351]: Received disconnect from 159.65.144.233 port 49715:11: Normal 
May 26 07:32:22 instance-1 sshd[20351]: Invalid user admin from 159.65.144.233 port 49715

我需要从该文件中读取数据行,并将排序列表打印到包含无效用户名的文件中,每行一个,如下所示。

nagios 
admin

我已经取得了进步。我删除了用户名前后的所有内容,但是我需要对其进行排序。

    errors = []                   # The list where we will store results.
    linenum = 0
    substr = "Invalid user"          # Substring to search for.
    with open ('auth.log', 'rt') as myfile:
        for line in myfile:
        linenum += 1
        if line.find(substr) != -1:
            errors.append(line.rstrip('\n'))
    fake_users = []
    for line in users:
        fake_users = (line.split("Invalid user")[1])
        print(fake_users.partition("from")[0])

谢谢

【问题讨论】:

  • 到目前为止你尝试了什么?请分享您的代码。

标签: python


【解决方案1】:

您可以考虑在每个 Invalid user 行之后使用正则表达式来获取用户名。正则表达式可能有点矫枉过正,但在这里对我来说似乎很合适。这是我的意思的一个例子:

import re
input_ = '''
May 26 07:29:20 instance-1 sshd[20327]: Disconnected from 61.147.247.146 
port 45177 [preauth]
May 26 07:32:22 instance-1 sshd[20351]: Invalid user nagios from 
159.65.144.233 port 49715
May 26 07:32:22 instance-1 sshd[20351]: input_userauth_request: invalid 
user nagios [preauth]
May 26 07:32:23 instance-1 sshd[20351]: Received disconnect from 
159.65.144.233 port 49715:11: Normal 
May 26 07:32:22 instance-1 sshd[20351]: Invalid user admin from 
159.65.144.233 port 49715
'''

pattern = re.compile(r'Invalid user (\b\w+\b)')
for line in input_.split('\n'):
    match = pattern.search(line)
    if match is not None:
        print(match.group(1))

我发现了一个有点类似的问题:match an element of every line

如果您不熟悉 Python 中的正则表达式,请查看文档 here。

【讨论】:

    【解决方案2】:

    除了 NBartley 的回答之外,在我看来,您的主要问题是如何对您的用户名数组进行排序。见下文。

    import re
    input_ = '''
    May 26 07:29:20 instance-1 sshd[20327]: Disconnected from 61.147.247.146 
    port 45177 [preauth]
    May 26 07:32:22 instance-1 sshd[20351]: Invalid user nagios from 
    159.65.144.233 port 49715
    May 26 07:32:22 instance-1 sshd[20351]: input_userauth_request: invalid 
    user nagios [preauth]
    May 26 07:32:23 instance-1 sshd[20351]: Received disconnect from 
    159.65.144.233 port 49715:11: Normal 
    May 26 07:32:22 instance-1 sshd[20351]: Invalid user admin from 
    159.65.144.233 port 49715
    '''
    output = []
    pattern = re.compile(r'Invalid user (\b\w+\b)')
    for line in input_.split('\n'):
        match = pattern.search(line)
        if match is not None:
            output+= [match.group(1)]
    output = output.sort()
    print(output)
    

    【讨论】:

    • 除了排序,我修改了 NBartley 的,效果很好。但是 output.sort 不适用于您的。它多次返回 None 。谢谢大家的帮助。
    猜你喜欢
    • 2015-02-22
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多