Table of Contents
In this section we will use pygnmi, python gnmi client library, to send “set”, “delete” and “subscribe” commands to network devices.
The gnmi commands “capabilities” and “get” are discussed in the previous section.
pygnmi subscribe and set code examples
As we have discussed in the previous section, we use nonrir beside gnmi client python library to use inventory advantage of nornir library.
Nornir inventory files and config file are already discussed and prepared in the previous section and we will use them also for the demonstration of pygnmi set, delete and subscribe commands.
pygnmi set command code example
This is the script to change the description of the interface.
The Example and payload to change the description of the interface, is taken from arista networks github community. The only changes that I have made is to combine it with nornir capability.
In the header of the script, nornir and gNMIclient are imported.
A “client” object is created for each device from “gNMIclient” task and inventory information of each device.
Then “set” command is used to change the configuration. The payload of the new configuration is uploaded through “update” parameter.
The payload itself is stored in “u” variable which changes the description of ethernet1 interface.
The function is then run through nornir “nr” object initialized in the beginning of the script.
Now let’s run the script to see the result.
pygnmi delete command code example
In the next script we will delete the description of the interface that we have already created.
For deleting the configuration, we use still gnmi “set” command. The only difference is that we use “delete” parameter instead of “update” parameter.
The path to the description in the Yang data model is specified in the “delete” parameter.
The path to the description section in yang data model is configured inside “path1” variable.
Then the function to delete the description is executed through nornir object.
Let’s run the script to make sure that description will be deleted.
pygnmi subscribe command code example
And finally, as the last script in gnmi protocol, we use the most important gnmi command, “subscribe”.
With “subscribe” command, any user or application can subscribe itself to any data in yang data model. Then the data will be streamed to the subscriber intervalley or whenever changes.
Two examples are prepared for the subscribe command. The mode as stream mode and encoding type as json is configured.
The first example is to subscribe to ethernet1 interface incoming statistics to be streamed every 10 seconds.
The second example stream interface etheret1 status whenever it changes.
Subscribe examples are run using “gNMIclient” client object, “subscribe” command and “subscribe” parameter.
Let’s run the script to see stream result.
To see interface status changes, we disable and enable the ethernet1 interface intentionally with shutdown and without shutdown command.