perf: 优化账号收集,使用正则处理结果

pull/10570/head
halo 2023-05-23 16:04:14 +08:00 committed by Jiangjie.Bai
parent 35e9c21ec5
commit b72f8a7241
2 changed files with 19 additions and 10 deletions

View File

@ -1,3 +1,5 @@
import re
from django.utils import timezone from django.utils import timezone
__all__ = ['GatherAccountsFilter'] __all__ = ['GatherAccountsFilter']
@ -27,18 +29,25 @@ class GatherAccountsFilter:
@staticmethod @staticmethod
def posix_filter(info): def posix_filter(info):
username_pattern = re.compile(r'^(\S+)')
ip_pattern = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
login_time_pattern = re.compile(r'\w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}')
result = {} result = {}
for line in info: for line in info:
data = line.split('@') usernames = username_pattern.findall(line)
if len(data) == 1: username = ''.join(usernames)
result[line] = {} if username:
result[username] = {}
else:
continue continue
ip_addrs = ip_pattern.findall(line)
if len(data) != 3: ip_addr = ''.join(ip_addrs)
continue if ip_addr:
username, address, dt = data result[username].update({'address': ip_addr})
date = timezone.datetime.strptime(f'{dt} +0800', '%b %d %H:%M:%S %Y %z') login_times = login_time_pattern.findall(line)
result[username] = {'address': address, 'date': date} if login_times:
date = timezone.datetime.strptime(f'{login_times[0]} +0800', '%b %d %H:%M:%S %Y %z')
result[username].update({'date': date})
return result return result
@staticmethod @staticmethod

View File

@ -5,7 +5,7 @@
ansible.builtin.shell: ansible.builtin.shell:
cmd: > cmd: >
users=$(getent passwd | grep -v nologin | grep -v shutdown | awk -F":" '{ print $1 }');for i in $users; users=$(getent passwd | grep -v nologin | grep -v shutdown | awk -F":" '{ print $1 }');for i in $users;
do k=$(last -w -F $i -1 | head -1 | grep -v ^$ | awk '{ print $1"@"$3"@"$5,$6,$7,$8 }') do k=$(last -w -F $i -1 | head -1 | grep -v ^$ | awk '{ print $0 }')
if [ -n "$k" ]; then if [ -n "$k" ]; then
echo $k echo $k
else else