def EstablishChannel(address, port, client_id, user, password):
    # Open a grpc channel to the device
    # creds = implementations.ssl_channel_credentials(open('/tmp/host.pem').read(), None, None)
    # channel = implementations.secure_channel(address, port, creds)

    try:
        channel = grpc.insecure_channel('%s:%d' % (address, port))

        auth_stub = authentication_service_pb2.LoginStub(channel)

        login_response = auth_stub.LoginCheck(
            authentication_service_pb2.LoginRequest(user_name=user,
                                                    password=password,
                                                    client_id=client_id),
            _TIMEOUT_SECONDS)

        if login_response.result == 1:
            print("Login successful")
            return channel
        else:
            print("Login failed")
            sys.exit(1)

    except Exception as tx:
        print(tx)
예제 #2
0
def _authenticateChannel(channel, user, passw, client_id):
    """
    This method authenticates the provided grpc channel.
    """
    sec_stub = authentication_service_pb2.LoginStub(channel)
    cred = authentication_service_pb2.LoginRequest(user_name=user,
                                                   password=passw,
                                                   client_id=client_id)
    res = sec_stub.LoginCheck(cred)
    return res
예제 #3
0
def Main():
    try:
        global device, user, password, grpc_port,request_id
        parser = argparse.ArgumentParser()
        parser.add_argument('-device', help='Input host name or ip ', type=str)
        parser.add_argument('-user', help='Input username for host', type=str)
        parser.add_argument('-password', help='Input password for host', type=str)
        parser.add_argument('-grpc_port', help='Input grpc port',type=str)
        parser.add_argument('-request_id', help='Input client request id',type=str)
        args, unknown = parser.parse_known_args()
        
        device = args.device or device
        user =  args.user or user
        password = args.password or password
        grpc_port = args.grpc_port or grpc_port
        request_id = args.request_id or request_id

        channel = grpc.insecure_channel(device+':'+grpc_port)
        stub = authentication_service_pb2.LoginStub(channel)
        try: 
            login_response = stub.LoginCheck(authentication_service_pb2.LoginRequest(user_name=user,password=password, client_id=request_id), timeout)
        except Exception as e:
            print('request id given is in use, using new one')  
            login_response = stub.LoginCheck(authentication_service_pb2.LoginRequest(user_name=user,password=password, client_id='new'), timeout) 

        if login_response.result :
            print "[INFO] Connected to gRPC Server:",+login_response.result
        else:
            print "[ERROR] gRPC Server Connection failed!!!",+login_response.result

        mgd = management_service_pb2.ManagementRpcApiStub(channel)

        op_command = "show version"
        op = ExecuteOpCommandRequest(cli_command = op_command, out_format = 2, request_id = 1000)
        result = mgd.ExecuteOpCommand(op, timeout)
        response ='' 
        for i in result:
            response += i.data
        if i.status == 0:
            print 'Invoked ExecuteOpCommand API return code = ', i.status
            print 'Invoked ExecuteOpCommand API return code = ', i.data 
        else:
            print 'Something Went Wrong !!! Data not received'
    except AbortionError as ex: 
        print ('The application got closed abruptly!!!')
        print ('Got exception: %s' % ex.message)
        print (traceback.print_exc())
        print (ex.code)
        print (ex.details)
    except Exception as ex:
        print (ex.message)
        print (traceback.print_exc())
    return
예제 #4
0
def _authenticateChannel(channel, user, passw, client_id):
    """
    This method authenticates the provided grpc channel.
    """
    sec_stub = authentication_service_pb2.LoginStub(channel)
    try:
        login_response = stub.LoginCheck(
            authentication_service_pb2.LoginRequest(user_name=user,
                                                    password=password,
                                                    client_id=request_id),
            timeout)
    except Exception as e:
        print('request id given is in use, using new one')
        res = sec_stub.LoginCheck(authentication_service_pb2.LoginRequest(
            user_name=user, password=password, client_id='request'),
                                  timeout=60)
    return res
예제 #5
0
def Main():
    try:
        parser = argparse.ArgumentParser()

        parser.add_argument('-d',
                            '--device',
                            help='Input hostname',
                            required=True)
        parser.add_argument('-t',
                            '--timeout',
                            help='Input time_out value',
                            required=True,
                            type=int)
        parser.add_argument('-u',
                            '--user',
                            help='Input username',
                            required=True)
        parser.add_argument('-pw',
                            '--password',
                            help='Input password',
                            required=True)

        args = parser.parse_args()

        #Establish grpc channel to jet router
        creds = grpc.ssl_channel_credentials(
            open('/tmp/RSA2048.pem').read(), None, None)
        channel = grpc.secure_channel(args.device + ":32767",
                                      creds,
                                      options=((
                                          'grpc.ssl_target_name_override',
                                          _HOST_OVERRIDE,
                                      ), ))

        #create stub for authentication services
        stub = authentication_service_pb2.LoginStub(channel)
        #Authenticate
        login_request = authentication_service_pb2.LoginRequest(
            user_name=args.user, password=args.password, client_id="SampleApp")
        login_response = stub.LoginCheck(login_request, args.timeout)

        #Check if authentication is successful
        if login_response.result == True:
            print "[INFO] Connected to gRPC Server:"
            print login_response.result
        else:
            print "[ERROR] gRPC Server Connection failed!!!"
            print login_response.result

        #Create stub for management services
        stub = management_service_pb2.ManagementRpcApiStub(channel)
        print "[INFO] Connected to JSD and created handle to mgd services"

        for i in range(1):
            #Provide API request details
            op_xml_command = "<get-system-uptime-information>" \
            "</get-system-uptime-information>"
            op = management_service_pb2.ExecuteOpCommandRequest(
                xml_command=op_xml_command, out_format=2, request_id=1000)
            # Invoke API
            result = stub.ExecuteOpCommand(op, 100)
            # Check API response like status and output
            for i in result:
                print "[INFO] Invoked ExecuteOpCommand API return code = "
                print i.status
                print "[INFO] Return output in CLI format = "
                print i.data
    except Exception as ex:
        print ex
예제 #6
0
def Main():
    try:
        parser = argparse.ArgumentParser()

        parser.add_argument('-d',
                            '--device',
                            help='Input hostname',
                            required=True)
        parser.add_argument('-t',
                            '--timeout',
                            help='Input time_out value',
                            required=True,
                            type=int)
        parser.add_argument('-u',
                            '--user',
                            help='Input username',
                            required=True)
        parser.add_argument('-pw',
                            '--password',
                            help='Input password',
                            required=True)

        args = parser.parse_args()

        #Establish grpc channel to jet router
        creds = grpc.ssl_channel_credentials(
            open('/tmp/RSA2048.pem').read(), None, None)
        channel = grpc.secure_channel(args.device + ":32767",
                                      creds,
                                      options=((
                                          'grpc.ssl_target_name_override',
                                          _HOST_OVERRIDE,
                                      ), ))

        #create stub for authentication services
        stub = authentication_service_pb2.LoginStub(channel)
        #Authenticate
        login_request = authentication_service_pb2.LoginRequest(
            user_name=args.user, password=args.password, client_id="SampleApp")
        login_response = stub.LoginCheck(login_request, args.timeout)

        #Check if authentication is successful
        if login_response.result == True:
            print "[INFO] Connected to gRPC Server:"
            print login_response.result
        else:
            print "[ERROR] gRPC Server Connection failed!!!"
            print login_response.result

        #Create stub for management services
        mgd_stub = management_service_pb2.ManagementRpcApiStub(channel)
        print "[INFO] Connected to JSD and created handle to mgd services"
        set_config = """<configuration-set>
                     set system location building bldg-name
                     set system location floor 4
                     </configuration-set>"""
        cfg_req = ExecuteCfgCommandRequest(request_id=1,
                                           text_config=set_config,
                                           load_type=4,
                                           commit=ConfigCommit(commit_type=1))
        CONFIG_RESULT = mgd_stub.ExecuteCfgCommand(cfg_req, timeout=60)
        print "Configuration Status", CONFIG_RESULT

    except Exception as ex:
        print ex
예제 #7
0
clientid = socket.gethostname()

try:
    # open gRPC channel
    print("")
    print(__file__)
    print("Trying to Login to",
          args.target,
          "port",
          args.port,
          "as user",
          args.username,
          "... ",
          end='')
    channel = grpc.insecure_channel('%s:%d' % (args.target, args.port))
    auth_stub = authentication_service_pb2.LoginStub(channel)
    login_response = auth_stub.LoginCheck(
        authentication_service_pb2.LoginRequest(user_name=args.username,
                                                password=args.password,
                                                client_id=clientid),
        _JET_TIMEOUT)

    if login_response.result == 1:
        print("Login successful")
    else:
        print("Login failed")
        sys.exit(1)

except Exception as tx:
    print(tx)