diff --git a/apps/accounts/automations/gather_accounts/filter.py b/apps/accounts/automations/gather_accounts/filter.py index c6db6dbd4..f3b111d36 100644 --- a/apps/accounts/automations/gather_accounts/filter.py +++ b/apps/accounts/automations/gather_accounts/filter.py @@ -1,3 +1,5 @@ +import re + from django.utils import timezone __all__ = ['GatherAccountsFilter'] @@ -27,18 +29,25 @@ class GatherAccountsFilter: @staticmethod 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 = {} for line in info: - data = line.split('@') - if len(data) == 1: - result[line] = {} + usernames = username_pattern.findall(line) + username = ''.join(usernames) + if username: + result[username] = {} + else: continue - - if len(data) != 3: - continue - username, address, dt = data - date = timezone.datetime.strptime(f'{dt} +0800', '%b %d %H:%M:%S %Y %z') - result[username] = {'address': address, 'date': date} + ip_addrs = ip_pattern.findall(line) + ip_addr = ''.join(ip_addrs) + if ip_addr: + result[username].update({'address': ip_addr}) + login_times = login_time_pattern.findall(line) + 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 @staticmethod diff --git a/apps/accounts/automations/gather_accounts/host/posix/main.yml b/apps/accounts/automations/gather_accounts/host/posix/main.yml index 910b1213d..d3cbe9c75 100644 --- a/apps/accounts/automations/gather_accounts/host/posix/main.yml +++ b/apps/accounts/automations/gather_accounts/host/posix/main.yml @@ -5,7 +5,7 @@ ansible.builtin.shell: cmd: > 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 echo $k else