|
| 1 | +""" |
| 2 | +GRPC Client with command line options |
| 3 | +
|
| 4 | +Usage: |
| 5 | + client.py -i <router_IP> -p <port> -u <user> -pw <password> -r <rpc> [--file <json_file>] |
| 6 | + client.py (-h | --help) |
| 7 | +
|
| 8 | +Options: |
| 9 | + -h --help Show this help |
| 10 | + --version Show version |
| 11 | + -i IP address of the router which the client will connect to |
| 12 | + -p Router gRPC server will be running on some TCP port, generally 57400 |
| 13 | + -u Username to connect with |
| 14 | + -pw User's password |
| 15 | + -r RPC call to make. Valid RPCS: get-oper, get-config, merge-config, replace-config |
| 16 | + --file Optional json file for filtered namespace requests. Client is expecting these files to be stored in json/ folder |
| 17 | +
|
| 18 | +Example: |
| 19 | +python cli.py -i 192.168.122.214 -p 57400 -u cisco -pw cisco -r get-oper --file json/get-oper-mpls-te.json |
| 20 | +
|
| 21 | +Note: Version 1.0 supports get-oper, get-config, and merge-config RPCs, replace-config hopefully coming soon |
| 22 | +""" |
| 23 | + |
| 24 | +import json |
| 25 | +from grpc.framework.interfaces.face.face import AbortionError |
| 26 | +import sys |
| 27 | +sys.path.insert(0, '../') |
| 28 | +from lib.cisco_grpc_client import CiscoGRPCClient |
| 29 | + |
| 30 | +from docopt import docopt |
| 31 | + |
| 32 | +def main(): |
| 33 | + |
| 34 | + __version__ = 'GRPC_Client 1.0' |
| 35 | + arguments = docopt(__doc__, version=__version__) |
| 36 | + |
| 37 | + IP = arguments['<router_IP>'] |
| 38 | + TCP_PORT = int(arguments['<port>']) |
| 39 | + user = arguments['<user>'] |
| 40 | + password = arguments['<password>'] |
| 41 | + RPC = arguments['<rpc>'] |
| 42 | + |
| 43 | + client = CiscoGRPCClient(IP, TCP_PORT, 10, user, password) |
| 44 | + |
| 45 | + if RPC == "get-oper": |
| 46 | + |
| 47 | + if arguments['--file']: |
| 48 | + file = arguments['--file'] |
| 49 | + path = open(file).read() |
| 50 | +# path = open('json/' + file).read() |
| 51 | + else: |
| 52 | + path = 'Error' |
| 53 | + print( |
| 54 | + 'get-oper argument must include --file option and json file to filter yang operational namespace' |
| 55 | + ) |
| 56 | + try: |
| 57 | + err, result = client.getoper(path) |
| 58 | + if err: |
| 59 | + print err |
| 60 | + print result |
| 61 | + except AbortionError: |
| 62 | + print( |
| 63 | + 'Unable to connect to local box, check your gRPC destination.' |
| 64 | + ) |
| 65 | + |
| 66 | + if RPC == "get-config": |
| 67 | + |
| 68 | + if arguments['--file']: |
| 69 | + file = arguments['--file'] |
| 70 | + path = open(file).read() |
| 71 | +# path = open('json/' + file).read() |
| 72 | + else: |
| 73 | + path = "" |
| 74 | + |
| 75 | + try: |
| 76 | + err, result = client.getconfig(path) |
| 77 | + if err: |
| 78 | + print err |
| 79 | + print result |
| 80 | + except AbortionError: |
| 81 | + print( |
| 82 | + 'Unable to connect to local box, check your gRPC destination.' |
| 83 | + ) |
| 84 | + |
| 85 | + if RPC == "merge-config": |
| 86 | + |
| 87 | + if arguments['--file']: |
| 88 | + file = arguments['--file'] |
| 89 | + path = open(file).read() |
| 90 | + else: |
| 91 | + path = 'Error' |
| 92 | + print( |
| 93 | + 'get-oper argument must include --file option and json file to filter yang operational namespace' |
| 94 | + ) |
| 95 | + try: |
| 96 | + err = client.mergeconfig(path) |
| 97 | + if err: |
| 98 | + print err |
| 99 | + #print result |
| 100 | + except AbortionError: |
| 101 | + print( |
| 102 | + 'Unable to connect to local box, check your gRPC destination.' |
| 103 | + ) |
| 104 | + |
| 105 | + if RPC == "replace-config": |
| 106 | + |
| 107 | + if arguments['--file']: |
| 108 | + file = arguments['--file'] |
| 109 | + path = open(file).read() |
| 110 | + else: |
| 111 | + path = 'Error' |
| 112 | + print( |
| 113 | + 'get-oper argument must include --file option and json file to filter yang operational namespace' |
| 114 | + ) |
| 115 | + try: |
| 116 | + err = client.replaceconfig(path) |
| 117 | + if err: |
| 118 | + print err |
| 119 | + except AbortionError: |
| 120 | + print( |
| 121 | + 'Unable to connect to local box, check your gRPC destination.' |
| 122 | + ) |
| 123 | + |
| 124 | +if __name__ == '__main__': |
| 125 | + main() |
0 commit comments