0

Simple task I am having a bit of trouble with. What I want to do is set local environment variables and perform UNIX style shell expansion in my cfg file. I am passing this file to ConfigParser() in my python script.

My config file - snmp-manage.cfg:

[system_1]
description=Arch_Linux
address=${IP_ADDRESS}
port=${PORT}
communityro=${COMMUNITYRO}

[check_1]
description=WLAN incoming traffic
oid=1.3.6.1.2.1.2.2.1.10.3
system=system_1

[check_2]
description=WLAN outgoing traffic
oid=1.3.6.1.2.1.2.2.1.16.3
system=system_1

The code that is parsing this file - snmp-manage.py:

def main(conf_file=""):
    if not conf_file:
        sys.exit(-1)
    config = SafeConfigParser()
    config.read(conf_file)
    snmp_manager = SnmpManager()

    # Quick test that config file is read and being parsed correctly. Also that
    # contents of the file are present and correct.
    # print(config.sections())
    for system in [s for s in config.sections() if s.startswith('system')]:
        snmp_manager.add_system(system,
                                config.get(system, 'description'),
                                config.get(system, 'address'),
                                config.get(system, 'port'),
                                config.get(system, 'communityro'))
    for check in [c for c in config.sections() if c.startswith('check')]:
        snmp_manager.add_check(check,
                               config.get(check, 'oid'),
                               config.get(check, 'description'),
                               config.get(check, 'system'))
    # snmp_manager.show_systems()
    snmp_manager.query_all_systems()


if __name__ == '__main__':
    main(conf_file="snmp-manage.cfg")

Expected results example:

address = <ip_address>

Actual results:

address = ${IP_ADDRESS}

(my parser is not reading the environment variables.)

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Do the answers to [ConfigParser and String interpolation with env variable](https://stackoverflow.com/questions/26586801/configparser-and-string-interpolation-with-env-variable) address what you're asking? – martineau Dec 17 '21 at 11:45
  • @martineau yes, this did work for me. Thanks! – Gianni Crivello Dec 18 '21 at 09:00

0 Answers0