Return section name if 2 specific parameters are found

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • unixpipe
    New Member
    • Nov 2014
    • 1

    Return section name if 2 specific parameters are found

    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_c hange = password_must_b e_changed
    Password_expire s = never
    Password_inacti ve = never
    Account_expires = never
    Minimum_number_ of_days_between _password_chang e = 7
    Maximum_number_ of_days_between _password_chang e = 90
    Number_of_days_ of_warning_befo re_password_exp ires = 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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Following is an example:
    Code:
    def is_account_open(data):
        dataDict = {}
        dataList = data.split("\n")
        username = dataList[0].strip("[]")
        for line in dataList[1:]:
            key, value = line.split("=")
            dataDict[key.strip()] = value.strip()
    
        if all((dataDict["Account_expires"] == "never",
                dataDict["Password_expires"] == "never")):
            return username
        return False
    
    if __name__ == "__main__":
        data = """[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"""
        print is_account_open(data)

    Comment

    Working...