-
Notifications
You must be signed in to change notification settings - Fork 10
/
password_age.py
executable file
·51 lines (46 loc) · 1.91 KB
/
password_age.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
'''
Started on this script and then like a day later Jamf released a similar script
which does most of what I wanted so I shamelessly raided it:
https://github.com/jamfit/Current-User-Password-Age/blob/master/Current-User-Password-Age.py
'''
import os
import sys
import subprocess
import plistlib
import datetime
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
def get_date_changed(username, creation_date=None):
'''Gets the date of last password change'''
# return None
# for 10.10+ or non-migrated accounts
task = subprocess.check_output(['/usr/bin/dscl', '.', 'read', 'Users/' + username, 'accountPolicyData'])
plist = plistlib.readPlistFromString('\n'.join(task.split()[1:]))
if 'creationTime' in plist.keys():
creation_date = datetime.datetime.utcfromtimestamp(plist['creationTime']).date()
if 'passwordLastSetTime' in plist.keys():
return datetime.datetime.utcfromtimestamp(plist['passwordLastSetTime']).date()
else:
# for 10.9.x and lower, or migrated accounts
task = subprocess.Popen(['/usr/bin/dscl', '.', 'read', 'Users/' + username, 'PasswordPolicyOptions'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = task.communicate()
if not err:
plist = plistlib.readPlistFromString('\n'.join(out.split()[1:]))
if 'passwordLastSetTime' in plist.keys():
return plist['passwordLastSetTime'].date()
return creation_date
def main():
username = SCDynamicStoreCopyConsoleUser(None, None, None)[0]
# username = 'luser'
if username:
changed = get_date_changed(username)
if changed:
today = datetime.datetime.utcnow().date()
pw_age = (today - changed).days
print pw_age
else:
print 'Undetermined'
if __name__ == '__main__':
main()