How it works…

In step 1, we create a Python environment with the required modules imported into the namespace. In step 2, we define a simple exception handler to print a message and exit if and when things go wrong.

As we move to step 3, we set up the execution of our script by parsing the command line arguments in order to set the runtime options. We use the argparse module to do the work for us here, because it’s quick, convenient, and is included in the standard Python distribution. The only change from previous recipes using Python’s argparse compared to this is that we demote the target device to be specified by a –t switch rather than the main argument. This is because we want the main argument to be the destination to query.

In step ...

Get JUNOS Automation Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.