Skip to content

Instantly share code, notes, and snippets.

@jwbensley
Last active November 15, 2016 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwbensley/cd2f5bfacee321328ca638b5b5523bd9 to your computer and use it in GitHub Desktop.
Save jwbensley/cd2f5bfacee321328ca638b5b5523bd9 to your computer and use it in GitHub Desktop.
Example gRPC client for IOS-XR which loads a json plain text file and sends it over gRPC, using certificate authentication.
# Code from https://github.com/cisco-grpc-connection-libs/ios-xr-grpc-python/blob/master/examples/grpc_cfg.py
from grpc.framework.interfaces.face.face import AbortionError
import json
from time import sleep
import sys
sys.path.insert(0, '../')
from lib.cisco_grpc_client import CiscoGRPCClient
class Example(object):
def __init__(self):
creds = open('/home/bensley/Python/pyb_oc/IOS-XRv621-grpc.pem').read()
options = 'ems.cisco.com'
self.client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant', creds, options)
def get(self):
path = '{"Cisco-IOS-XR-ipv4-bgp-cfg:bgp": [null]}'
result = self.client.getconfig(path)
try:
err, result = self.client.getconfig(path)
if err:
print err
print json.dumps(json.loads(result), indent=4)
except AbortionError:
print(
'Unable to connect to local box, check your gRPC destination.'
)
def merge(self):
path = open('snips/broken_restart_timer.json').read()
try:
response = self.client.mergeconfig(path)
if response.errors:
err = json.loads(response.errors)
print err
except AbortionError:
print(
'Unable to connect to local box, check your gRPC destination.'
)
def main():
example = Example()
print "Config before merge:\n"
example.get()
example.merge()
print "Config after merge:\n"
example.get()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment