【发布时间】: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