View Only
last person joined: 3 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  pyEZ to parse config from textfile

    Posted 07-19-2021 19:37
    Anyone happen to know if pyez supports reading a configuration from a text file and parsing it. I basically need a way in python to parse config files that a 3rd party system pulls from a set of routers nightly for backups. I don't currently have the option to change the backup format to JSON or XML within the system, so I'm wondering if this is possible with the pyex library. So far I'm only seeing supported methods to accomplish this when connecting to live devices. Thanks in advance for your time.


  • 2.  RE: pyEZ to parse config from textfile

    Posted 07-20-2021 05:02
    You can load the config from multiple sources as described here. With your browser, find the section on "Specify the Location of the Configuration Data".



  • 3.  RE: pyEZ to parse config from textfile

    Posted 07-20-2021 17:34
    Hi Elvin,

    Thanks for taking the time to respond. I previously saw the link you referenced, but the issue I have is that it seems like there is a requirement to create a device instance, which requires a hostname or IP address. And I don't have a live device to connect to. I just want to use pyez to parse text file configuration output (curly braces format) to extract values like Interface names, IP addresses, hostname,  etc from the config. I'm not looking to interact with a live device. I just want to use pyez as a parser to extract data from a config in a text file . Hopefully this clarifies things a bit. 

    Thanks again,


  • 4.  RE: pyEZ to parse config from textfile

    Posted 07-21-2021 08:22
    Hi Dave,

    I don't think this is possible at all. If my reading of the pyez code is correct, the library doesn't actually do any parsing of the config at all - what it does do is it takes the config in one of the supported formats, and uses an rpc call against the device to load the candidate config there, effectively letting Junos do the config parsing. 

    In case you're interested, take a look at the source of the Config class from jnpr.junos.utils.config, specifically look for the load() method which is what you normally use to load the config to the device. 

    The load() method, if it's not provided explicitly with the format of the config, tries to figure it out based on the combination of file extensions, regular expressions, etc (this is done using the nested private helper methods _lformat_byext(), _lset_format(), _lset_fromfile() etc), and then constructs the rpc call with all required options to load the config file to the device. The only processing done locally is for the Jinja2 templates, before sending the resulting config to the device.  

    All configuration parsing, validation, etc is done on the device. 

    If there's anything specific you need to parse out of the config files you have available, perhaps just using normal text processing would be the way to do this. The curly braces format is structured enough that it should be possible. I would personally look at trying to beat the file into something you could read into a python dict object and go from there.