I have stored output of the command chage -l user
in variable output
and need to check if user account password is not expired or will expire within 90 days.
import re
output = '''Last password change : Aug 26, 2017
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
'''
regexp = re.compile(r'Password expires [ ]*(:*?)')
match = regexp.match(output)
if match:
VALUE = match.group(2)
Now, I need to store the value in a variable to proceed forward but not able to do it. Above is my code. Ideally VALUE should have "never".
re.match
will not look for the pattern throughout the string, but will rather match for it right at the start of the string (as if the regex started with ^
). So you need re.search
, which will check for the pattern throughout the target string:
import re
output = '''Last password change : Aug 26, 2017
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
'''
regexp = re.compile(r'Password expires\s+: (.*)')
match = regexp.search(output)
if match:
VALUE = match.group(1)
print(VALUE)