View Only
last person joined: 3 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  is there a way to capture commit() output when using PyEZ?

    Posted 07-28-2021 04:43
    I am working on a script that eventually will connect to a number of EX switches, whose IPs are contained in an Excel spreadsheet, and make a config change. I am trying to account for possible commit errors, extract unique details of errors and write them to the same file, so that later it would be easy to know and fix the specific issues with each switch where a config failed.

    However, I am having a hard time finding info on how I can capture output of an unsuccessful commit operation that I would normally see when SSH'd into a switch or connected via console. For example, I have a 2-switch virtual chassis where storage is full on one of the switches, and thus commit fails. It fails with the following messages:
    commit error message as seen in CLI
    I would like a chance to capture this commit error and have a chance to distill useful info from it (like a hint that there might be a storage space issue) and write that out to a file.
    Is there a way to do this?
    I understand that I can use CommitError from jnpr.junos.exception, but the text of that error is not descriptive enough of the specific issue encountered. When I use  this with try/except construction I only get:
    commit error captured with jnpr.junos.exception
    I also tried using commit(detail=True) option, but that does not even indicate an error, as far as I can tell:
    commit output captured using commit(detail=True)So, is there a way to just capture output I would see in CLI in an event of an unsuccessful commit? 


  • 2.  RE: is there a way to capture commit() output when using PyEZ?

    Posted 07-28-2021 05:41
    I think what you are looking for is the commit check function.  This would be run after you load the intended change.

    When it returns true you can continue normally.

    When it returns an error save that out to your file, rollback the change and move on to the next ip in the inventory.

       def commit_check(self):
            Perform a commit check.  If the commit check passes, this function
            will return ``True``.  If the commit-check results in warnings, they
            are reported and available in the Exception errs.
            :returns: ``True`` if commit-check is successful (no errors)
            :raises CommitError: When errors detected in candidate configuration.
                                 You can use the Exception errs variable
                                 to identify the specific problems
            :raises RpcError: When underlying ncclient has an error

    Steve Puluka BSEET - Juniper Ambassador
    IP Architect - DQE Communications Pittsburgh, PA (Metro Ethernet & ISP)

  • 3.  RE: is there a way to capture commit() output when using PyEZ?

    Posted 07-28-2021 14:09
    @spuluka  Thank you for considering my question and for your response. Did you have a chance to test this out or were you just going with the commit_check description?

    If you did test this out, and you actually got the output details I was seeking to capture (basically, the output I would have seen in CLI, containing the hint about the storage being full, in this case) then perhaps I am not doing this right, as am still missing those.

    Using commit_check​ does not seem to provide any information that is beyond or different from what I was getting with commit(). I have not used 'commit check' extensively when working in CLI, and while I am sure there must be some subtle differences in how Junos processes the two commands, operationally there does not seem to be much difference (other than that a successful 'commit check' must be followed up with a commit). Same seems to be the case here. If commit or commit_check/commit fail, I get basically the same error message, I am still missing output details I would have seen in CLI, and I still need to rollback config changes.

    With commit():
    commit() error

    With commit_check():
    commit_check() error

    Again, what I wanted to capture is output I would have seen in CLI, this time for commit check:
    CLI commit check error output
    The jnpr.junos.exception is a nice tool the PyEZ makes available, but in this case anyway the way it captures an error misses out on some more specific information. Here it seems it captures error text just from the error on the virtual-chassis master, ignoring the more informative error text from member0 on which the error actually occurs. 

    I do not mind attempting to write my own code to parse CLI output to extract the info I want. So, my question stands, perhaps rephrased more precisely now:
    Is there a way, using PyEZ, to capture output of the commit() command that one would see were one executing commit command directly on the device?

    This is possibly not the only command whose output is massaged and cleaned up by PyEZ and something is lost in the process. Is there a way to capture raw/CLI output?

    Thank you!​​​​


  • 4.  RE: is there a way to capture commit() output when using PyEZ?

    Posted 07-28-2021 19:18
    In our application we were running and writing to file the show compare and the the commit check results.  So the pass/fail was sufficient.

    If you are going to do your own captures remember that the cli allows you to display results in json format (or xml in older versions of Junos).  This will make parsing and using the results easier.  Just pipe the command results into display json
    commit check | display json

    Steve Puluka BSEET - Juniper Ambassador
    IP Architect - DQE Communications Pittsburgh, PA (Metro Ethernet & ISP)

  • 5.  RE: is there a way to capture commit() output when using PyEZ?

    Posted 07-30-2021 13:41


    To be able to understand why that particular error message was not parsed by pyEZ, will need to look at the xml output of commit or commit check:

    commit check | display xml

    Can you share this output.


  • 6.  RE: is there a way to capture commit() output when using PyEZ?

    Posted 08-02-2021 05:15
    @ashvino Sure, the output you request is below. You will notice it is, of course, same in substance (and in xml format) as output of 'commit check' that I have posted in my previous messages (in json format) in this discussion. Hope this helps you.

    It might be of interest to the team that wrote PyEZ why the specifics of this error were not captured by their code exhaustively, if that even was their intent. However, I am afraid you misunderstood MY dilemma. The output of 'commit check | display xml' below, just like output of 'commit check' that I have posted previously are those command RUN IN CLI ON A DEVICE. My question is how do I capture the same output FROM PYTHON SCRIPT. Can you answer that question? 
    commit check xml outputThank you!


  • 7.  RE: is there a way to capture commit() output when using PyEZ?

    Posted 08-02-2021 05:16
    @spuluka Ahh you are talking about using jnpr.junos.utils.start_shell instead of the jnpr.junos.utils.config I have been using up to this point? Thank you for reminding me of the StartShell option - I now remember reading about it under the heading "Connecting and Retrieving Facts From a Device" here

    However, I do not recall and now cannot find StartShell mentioned as an option when CONFIGURING a device. The link above only mentions using StartShell run() method to execute OPERATIONAL mode commands. And 'cli -c' seems to allow one to enter only one line at a time. I tried this both from the shell on the device, and from a Python script that connects to a device. In both cases I can run operational commands like 'show version" or "show system uptime", those are not a problem.

    However, I am yet to be successful in using StartShell to run a configuration mode command like 'commit check', like you suggested.
    When I try something like this:

    The first like goes fine, and seem  like it lets me into configuration mode. But each line seems to be processed on its own and when it is processed, gets me back to shell. 'cli -c' seems to be expecting an operational mode command only, and does not know anything about 'set' or 'commit' commands:
    command output
    In hopes that perhaps 'cli' takes other, more appropriate for my case options than just '-c', I tried to find them. 'man cli' does not work, neither does 'cli -h', a random 'cli -e' produces the following, but I cannot find any appropriate documentation online.
    So, how were you suggesting I run "commit check | display json" to capture CLI output from a Pythin script? 
    Or could you point me to 'online documentation' for 'cli'?

    Thank you!


  • 8.  RE: is there a way to capture commit() output when using PyEZ?

    Posted 08-03-2021 12:10


    Could you try something like this if you see the error message in the received xml:

    from lxml import etree
    except CommitError as Err: