Skip to content

Commit 1e0d124

Browse files
sysadmin75Fizzadar
andcommitted
Add last login time to host.fact.users (#606)
* Add last login time to host.fact.users * Update pyinfra/facts/server.py Co-authored-by: Nick Barrett <[email protected]> * Updated test for suggested fix for lastlog login_time value Co-authored-by: Nick Barrett <[email protected]>
1 parent 6c0e1d9 commit 1e0d124

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

pyinfra/facts/server.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,24 +422,27 @@ class Users(FactBase):
422422
],
423423
'uid': user_id,
424424
'gid': main_user_group_id,
425+
'lastlog': last_login_time,
425426
},
426427
}
427428
'''
428429

429430
command = '''
430431
for i in `cat /etc/passwd | cut -d: -f1`; do
431432
ENTRY=`grep ^$i: /etc/passwd`;
432-
echo "$ENTRY|`id -gn $i`|`id -Gn $i`";
433+
LASTLOG=`lastlog -u $i | grep ^$i` | tr -s ' ';
434+
echo "$ENTRY|`id -gn $i`|`id -Gn $i`|$LASTLOG";
433435
done
434436
'''.strip()
435437

436438
default = dict
437439

438440
def process(self, output):
439441
users = {}
442+
rex = r'[A-Z][a-z]{2} [A-Z][a-z]{2} {1,2}\d+ .+$'
440443

441444
for line in output:
442-
entry, group, user_groups = line.split('|')
445+
entry, group, user_groups, lastlog = line.split('|')
443446

444447
if entry:
445448
# Parse out the comment/home/shell
@@ -452,6 +455,11 @@ def process(self, output):
452455
if group_name and group_name != group:
453456
groups.append(group_name)
454457

458+
# Parse lastlog info
459+
# lastlog output varies, which is why I use regex to match login time
460+
login = re.search(rex, lastlog)
461+
login_time = login.group() if login else None
462+
455463
users[entries[0]] = {
456464
'home': entries[5] or None,
457465
'comment': entries[4] or None,
@@ -460,6 +468,7 @@ def process(self, output):
460468
'groups': groups,
461469
'uid': int(entries[2]),
462470
'gid': int(entries[3]),
471+
'lastlog': login_time,
463472
}
464473

465474
return users

tests/facts/server.Users/mixed.json

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"output": [
3-
"root:x:0:0:root:/root:/bin/ks|wheel|wheel kmem sys tty operator staff guest",
4-
"_tesTy.test:x:1004:1004::/home/_tesTy.test:/bin/ksh|_tesTy.test|_tesTy.test pyinfra",
5-
"test.testy:x:1003:1003:Testy,,,:/home/test.testy:/bin/ksh|test.testy|",
6-
"noshell:x:1002:1002:noshell comment with spaces:/home/noshell:|noshell|",
7-
"nohome:x:1002:1002:nohome comment::/bin/bash|nohome|"
3+
"root:x:0:0:root:/root:/bin/ks|wheel|wheel kmem sys tty operator staff guest|root pts/0 Sat Jun 5 12:03:23 -0600 2021",
4+
"_tesTy.test:x:1004:1004::/home/_tesTy.test:/bin/ksh|_tesTy.test|_tesTy.test pyinfra|_tesTy.test pts/0 host-ip Sat Jun 12 13:43:42 -0600 2021",
5+
"test.testy:x:1003:1003:Testy,,,:/home/test.testy:/bin/ksh|test.testy||test.testy Fri Jun 11 22:26:04 -0600 2021",
6+
"noshell:x:1002:1002:noshell comment with spaces:/home/noshell:|noshell||noshell **Never logged in**",
7+
"nohome:x:1002:1002:nohome comment::/bin/bash|nohome||**Never logged in**"
88
],
9-
"command": "for i in `cat /etc/passwd | cut -d: -f1`; do\n ENTRY=`grep ^$i: /etc/passwd`;\n echo \"$ENTRY|`id -gn $i`|`id -Gn $i`\";\n done",
9+
"command": "for i in `cat /etc/passwd | cut -d: -f1`; do\n ENTRY=`grep ^$i: /etc/passwd`;\n LASTLOG=`lastlog -u $i | grep ^$i` | tr -s ' ';\n echo \"$ENTRY|`id -gn $i`|`id -Gn $i`|$LASTLOG\";\n done",
1010
"fact": {
1111
"root": {
1212
"home": "/root",
@@ -22,7 +22,8 @@
2222
"guest"
2323
],
2424
"uid": 0,
25-
"gid": 0
25+
"gid": 0,
26+
"lastlog": "Sat Jun 5 12:03:23 -0600 2021"
2627
},
2728
"_tesTy.test": {
2829
"home": "/home/_tesTy.test",
@@ -33,7 +34,8 @@
3334
"pyinfra"
3435
],
3536
"uid": 1004,
36-
"gid": 1004
37+
"gid": 1004,
38+
"lastlog": "Sat Jun 12 13:43:42 -0600 2021"
3739
},
3840
"test.testy": {
3941
"home": "/home/test.testy",
@@ -42,7 +44,8 @@
4244
"group": "test.testy",
4345
"groups": [],
4446
"uid": 1003,
45-
"gid": 1003
47+
"gid": 1003,
48+
"lastlog": "Fri Jun 11 22:26:04 -0600 2021"
4649
},
4750
"noshell": {
4851
"home": "/home/noshell",
@@ -51,7 +54,8 @@
5154
"group": "noshell",
5255
"groups": [],
5356
"uid": 1002,
54-
"gid": 1002
57+
"gid": 1002,
58+
"lastlog": null
5559
},
5660
"nohome": {
5761
"home": null,
@@ -60,7 +64,8 @@
6064
"group": "nohome",
6165
"groups": [],
6266
"uid": 1002,
63-
"gid": 1002
67+
"gid": 1002,
68+
"lastlog": null
6469
}
6570
}
6671
}

0 commit comments

Comments
 (0)