By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Return section name if 2 specific parameters are found

P: 1
Hi. I have a config file that is the output of the chage command formatted to my liking for python parsing. I would like to return the section name (in this case a username) if 'Account expires = never' and 'Password inactive = never'. Both must match to let me know that the account is still accessible. If only one parameter value of those is never, then it will mean the account is locked. I need to know if accounts are still open. I know there are other ways but I am specifically doing this in python.

Here is what the config file looks like:

[user1]
Last_password_change = password_must_be_changed
Password_expires = never
Password_inactive = never
Account_expires = never
Minimum_number_of_days_between_password_change = 7
Maximum_number_of_days_between_password_change = 90
Number_of_days_of_warning_before_password_expires = 14

I have a couple python scripts that can read me the sections, and items but I'm not sure how to generate what I need using an if statement for both parameters.
Nov 6 '14 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
Following is an example:
Expand|Select|Wrap|Line Numbers
  1. def is_account_open(data):
  2.     dataDict = {}
  3.     dataList = data.split("\n")
  4.     username = dataList[0].strip("[]")
  5.     for line in dataList[1:]:
  6.         key, value = line.split("=")
  7.         dataDict[key.strip()] = value.strip()
  8.  
  9.     if all((dataDict["Account_expires"] == "never",
  10.             dataDict["Password_expires"] == "never")):
  11.         return username
  12.     return False
  13.  
  14. if __name__ == "__main__":
  15.     data = """[user1]
  16. Last_password_change = password_must_be_changed
  17. Password_expires = never
  18. Password_inactive = never
  19. Account_expires = never
  20. Minimum_number_of_days_between_password_change = 7
  21. Maximum_number_of_days_between_password_change = 90
  22. Number_of_days_of_warning_before_password_expires = 14"""
  23.     print is_account_open(data)
Nov 6 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.