Ejemplo n.º 1
0
Archivo: util.py Proyecto: 3pings/aci
def query_asa(device, query_cmd):
    '''Read information back from the given device
    @param device: dict
        a device dictionary
    @param query: str, a show command cli, like "show run access-list bla"
    @return tuple with:
        1) response string from ASA; or None if cannot connect to the device
        2) any error or exception string, otherwise None
    @attention:  PLEASE do not use this API to make configuration change on the device
    '''
    if not device:
        error = "query_asa: fails to read response for command '%s'. Error: %s" % (
            query_cmd, 'empty device dictionary')
        env.debug(error)
        return (None, error)
    if not query_cmd.strip().startswith('show'):
        error = "query_asa: '%s' is not a show command, discarded" % query_cmd
        env.debug(error)
        return (None, error)
    try:
        dispatcher = HttpDispatch(device)
        messenger = dispatcher.make_shows_messenger([query_cmd])
        result = messenger.read()
        if result == 'Command failed\n':
            return None, None
        if '\nERROR: %' in result:
            return None, None
        return result, None
    except Exception as e:
        env.debug(
            "query_asa: fails to read response for command '%s'. Error: %s" %
            (query_cmd, e))
        return (None, str(e))
Ejemplo n.º 2
0
def query_asa(device, query_cmd):
    '''Read information back from the given device
    @param device: dict
        a device dictionary
    @param query: str, a show command cli, like "show run access-list bla"
    @return tuple with:
        1) response string from ASA; or None if cannot connect to the device
        2) any error or exception string, otherwise None
    @attention:  PLEASE do not use this API to make configuration change on the device
    '''
    if not device:
        error = "query_asa: fails to read response for command '%s'. Error: %s" % (query_cmd, 'empty device dictionary')
        env.debug(error)
        return (None, error)
    if not query_cmd.strip().startswith('show'):
        error = "query_asa: '%s' is not a show command, discarded" % query_cmd
        env.debug(error)
        return (None, error)
    try:
        dispatcher = HttpDispatch(device)
        messenger = dispatcher.make_shows_messenger([query_cmd])
        result =  messenger.read()
        if result == 'Command failed\n':
            return None, None
        if '\nERROR: %' in result:
            return None,None
        return result, None
    except Exception as e:
        env.debug("query_asa: fails to read response for command '%s'. Error: %s" % (query_cmd, e))
        return (None, str(e))