Python解析tac_plus配置文件

时间:2015-09-29 16:05:57

标签: python parsing configuration

我试图找到解析tac_plus的配置文件的最佳方法。能够将其转换为JSON格式以便将其存储在NoSQL DB中会很好。

以下是配置示例:

id = spawnd {
    listen = {
        port = 49
    }
    spawn = {
        instances min = 1
        instances max = 10
    }
    background = yes
}

id = tac_plus {
    access log = /var/log/tacacs/%Y/%m/%d/access.log
    accounting log = /var/log/tacacs/%Y/%m/%d/acct.log

    mavis module = external {    
        setenv LDAP_SERVER_TYPE = "microsoft"
        setenv LDAP_HOSTS = "ads01:3268 ads02:3268"
        setenv LDAP_SCOPE = sub
        setenv LDAP_BASE = "dc=example,dc=com"
        setenv LDAP_FILTER = "(&(objectclass=user)(sAMAccountName=%s))";
        setenv LDAP_USER = tacacs@example.com
        setenv LDAP_PASSWD = Secret123
        setenv AD_GROUP_PREFIX = tacacs
        setenv USE_TLS = 0
        exec = /usr/local/lib/mavis/mavis_tacplus_ldap.pl
    }

    login backend = mavis
    pap backend = mavis

    host = world {
        address = ::/0
        welcome banner = "Welcome\n"
        key = cisco
    }

    host = helpdesklab {
        address = 192.168.34.16/28
    }
    group = admin {
        default service = permit
        service = shell {
            default command = permit
            default attribute = permit
            set priv-lvl = 15
        }
    }  
    group = helpdesk {
        default service = permit
        service = shell {
            default command = permit
            default attribute = permit
            set priv-lvl = 1
        }
        enable = deny "enter code here"
        member = admin@helpdesklab
    }
}

1 个答案:

答案 0 :(得分:0)

您可以将其作为JSON存储在文件中,然后从那里加载它。

创建一个名为conf.json的文件:

{ 
  spawnd : { ... },
  tac_plus: { ... }
}

然后使用python加载它:

import json

with open('conf.json') as rdr:
    config = json.loads(rdr.read())

config['tac_plus']