Exemplo n.º 1
0
class Example(object):
    def __init__(self):
        self.client = CiscoGRPCClient('127.0.0.1', 57777, 10, 'vagrant', 'vagrant')
    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))
        except AbortionError:
            print(
                'Unable to connect to local box, check your gRPC destination.'
                )

    def replace(self):
        path = open('snips/bgp_start.json').read()
        try:
            response = self.client.replaceconfig(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 merge(self):
        path = open('snips/bgp_merge.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 delete(self):
        path = open('snips/bgp_start.json').read()
        try:
            response = self.client.deleteconfig(path)
            if response.errors:
                err = json.loads(response.errors)
                print err
        except AbortionError:
            print(
                'Unable to connect to local box, check your gRPC destination.'
                )
Exemplo n.º 2
0
def main():

    __version__ = 'GRPC_Client 1.0'
    arguments = docopt(__doc__, version=__version__)

    IP = arguments['<router_IP>']
    TCP_PORT = int(arguments['<port>'])
    user = arguments['<user>']
    password = arguments['<password>']
    RPC = arguments['<rpc>']

    client = CiscoGRPCClient(IP, TCP_PORT, 10, user, password)

    if RPC == "get-oper":

        if arguments['--file']:
            file = arguments['--file']
            path = open(file).read()
#            path = open('json/' + file).read()
        else:
            path = 'Error'
            print(
                'get-oper argument must include --file option and json file to filter yang operational namespace'
            )
        try:
            err, result = client.getoper(path)
            if err:
                print err
            print result
        except AbortionError:
            print(
                'Unable to connect to local box, check your gRPC destination.')

    if RPC == "get-config":

        if arguments['--file']:
            file = arguments['--file']
            path = open(file).read()


#            path = open('json/' + file).read()
        else:
            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 RPC == "merge-config":

        if arguments['--file']:
            file = arguments['--file']
            path = open(file).read()
        else:
            path = 'Error'
            print(
                'get-oper argument must include --file option and json file to filter yang operational namespace'
            )
        try:
            err = client.mergeconfig(path)
            if err:
                print err
            #print result
        except AbortionError:
            print(
                'Unable to connect to local box, check your gRPC destination.')

    if RPC == "replace-config":

        if arguments['--file']:
            file = arguments['--file']
            path = open(file).read()
        else:
            path = 'Error'
            print(
                'get-oper argument must include --file option and json file to filter yang operational namespace'
            )
        try:
            err = client.replaceconfig(path)
            if err:
                print err
        except AbortionError:
            print(
                'Unable to connect to local box, check your gRPC destination.')