Welcome to the lab, this guide will help you get started programming network devices using YANG and Python over gRPC. Using YANG and Python, the network is easier to program and configuration is more predictable.
-
Download the Github
git clone http://www.github.com/skkumaravel/devnet-1229
-
cd into directory and start the vagrant box.
cd devnet-1229 && vagrant up
-
Install python gRPC library
pip install iosxr_grpc
- Open up a new file called
example.py
in your favorite editor and add the following code.
from iosxr_grpc.cisco_grpc_client import CiscoGRPCClient
from grpc.framework.interfaces.face.face import AbortionError
def main():
client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant')
path = ''
try:
err, result = client.getconfig(path)
if err:
print(err)
print(result)
except AbortionError:
print('Unable to connect to local box, check your gRPC destination.')
if __name__ == '__main__':
main()
-
Run it.
python example.py
You should get the config of the entire box in yang format.
-
Test basic connectivity
vagrant ssh devbox
ping 11.1.1.10
exit
-
Change the path to the interface YANG path in
example.py
.path = '{"openconfig-interfaces:interfaces": {}}'
-
Lets run the output to a file and look at it.
python example.py > interface.json
-
Lets turn off a interface. Edit
interface.json
and change gig 0/0/0/0 enabled to false."name": "GigabitEthernet0/0/0/0", "config": { "name": "GigabitEthernet0/0/0/0", "type": "iana-if-type:ethernetCsmacd", "enabled": false },
-
Create
merge.py
and add the following code. This will allow you to open a file and merge the config in.from iosxr_grpc.cisco_grpc_client import CiscoGRPCClient from grpc.framework.interfaces.face.face import AbortionError def main2(): client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant') path = open('interface.json').read() try: err = client.mergeconfig(path) if err: print(err) except AbortionError: print('Unable to connect to local box, check your gRPC destination.') if __name__ == '__main__': main2()
-
Run the program to merge the config.
python merge.py
-
Test connectivity again.
vagrant ssh devbox
ping 11.1.1.10
exit
There are a lot more configuration options available in the library that can be used with automation.
Check out: