Ejemplo n.º 1
0
def main(argv):
    filters = [
        "ip_address_ip_address", "ip_static_gateway_list_ip_static_gateway",
        "subnet_mask"
    ]
    inputs = brcd_util.parse(argv, fibrechannel_switch, filters, validate)
    session = brcd_util.getsession(inputs)
    swobject = inputs['utilobject']
    if session['version'] < version.fosversion("8.2.1"):
        skipattributes = ""
        if swobject.peek_subnet_mask() is not None:
            skipattributes += "subnet-mask"
        if swobject.peek_ip_static_gateway_list_ip_static_gateway() != \
           "[]":
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "ip-static-gateway-list/ip-static-gateway"
        if len(skipattributes) > 0:
            print("Skipping following attributes:(", skipattributes,
                  ") for patch as the switch fos version[",
                  session['version'].to_string(),
                  "]is below the supported attribute version of 8.2.1")
        if swobject.peek_ip_address_ip_address() == "[]":
            pyfos_auth.logout(session)
            return
    result = _modify_switch_ip_config_params(session, swobject)
    pyfos_util.response_print(result)
    pyfos_auth.logout(session)
Ejemplo n.º 2
0
def dummy_session(user="******",
                  password="******",
                  ip="10.200.154.63",
                  https=None,
                  throttle=1.1,
                  verbose=0,
                  tokenArg=None,
                  sessionless=True):
    """
    This function creates a dummy session without any login to switch.
    returns a login session equivalent dictionary to be used for all REST
    operations.

    """
    auth = user + ":" + password
    auth_encoded = base64.b64encode(auth.encode())

    credential = {"Authorization": "Basic " + auth_encoded.decode()}
    ver9_0_0 = pyfos_version.fosversion("9.0.0")
    mysession = {
        "credential": credential,
        "ip_addr": ip,
        "user": user,
        "vfid": -1,
        "ishttps": https,
        "debug": verbose,
        "version": ver9_0_0,
        "throttle_delay": throttle,
        "auth_token": tokenArg,
        "sessionless": sessionless
    }
    return mysession
def main(argv):

    filters = []
    inputs = brcd_util.parse(argv, fibrechannel_switch, filters)

    session = brcd_util.getsession(inputs)
    if session['version'] < version.fosversion("8.2.1"):
        print("GET operation on ip-static-gateway-list/ip-static-gateway\
 and subnet_mask are supported from FOS v8.2.1")
    result = show_switch_ip_config(inputs['session'])
    pyfos_util.response_print(result)
    pyfos_auth.logout(session)
Ejemplo n.º 4
0
def main(argv):

    filters = []
    inputs = brcd_util.parse(argv, fibrechannel_switch, filters)

    session = brcd_util.getsession(inputs)
    if session['version'] < version.fosversion("8.2.1"):
        print("GET operation on dns-servers/dns-server\
 and domain-name are supported from FOS v8.2.1")
        pyfos_auth.logout(session)
        return
    result = show_dns_config(inputs['session'])
    pyfos_util.response_print(result)
    pyfos_auth.logout(session)
Ejemplo n.º 5
0
def main(argv):

    filters = []
    inputs = brcd_util.parse(argv, supportftp, filters)

    session = brcd_util.getsession(inputs)
    if session['version'] < version.fosversion("9.0.0"):
        print("GET operation on supportftp parameters\
 are supported from FOS v9.0.0")
        pyfos_auth.logout(session)
        return
    result = show_supportftp_params(inputs['session'])
    pyfos_util.response_print(result)
    pyfos_auth.logout(session)
Ejemplo n.º 6
0
def main(argv):
    filters = [
        "host", "user_name", "password", "remote_directory", "auto_enabled",
        "protocol", "port", "connectivity_check_interval"
    ]
    inputs = brcd_util.parse(argv, supportftp, filters, validate)
    supportftp_obj = inputs['utilobject']
    session = brcd_util.getsession(inputs)
    if session['version'] < version.fosversion("9.0.0"):
        skipattributes = ""
        if supportftp_obj.peek_host() is not None:
            skipattributes += "host"
        if supportftp_obj.peek_user_name() is not None:
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "user-name"
        if supportftp_obj.peek_password() is not None:
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "password"
        if supportftp_obj.peek_remote_directory() is not None:
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "remote-directory"
        if supportftp_obj.peek_auto_enabled() is not None:
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "auto-enabled"
        if supportftp_obj.peek_protocol() is not None:
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "protocol"
        if supportftp_obj.peek_port() is not None:
            skipattributes += "port"
        if supportftp_obj.peek_connectivity_check_interval() \
           is not None:
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "connectivity-check-interval"
        if len(skipattributes) > 0:
            print("Skipping following attributes:(", skipattributes,
                  ") for patch as the switch fos version[",
                  session['version'].to_string(),
                  "]is below the supported attribute version of 9.0.0")
        pyfos_auth.logout(session)
        return
    result = _modify_supportftp_params(session, supportftp_obj)
    pyfos_util.response_print(result)
    pyfos_auth.logout(session)
Ejemplo n.º 7
0
def main(argv):

    filters = ['clear_log']

    inputs = brcd_util.parse(argv, log_setting, filters, validate)

    session = brcd_util.getsession(inputs)
    if session['version'] < version.fosversion("9.0.0"):
        print("PATCH operation on clear-log attribute\
 is supported from FOS v9.0.0")
        pyfos_auth.logout(session)
        return

    result = _modify_clear_log(inputs['session'], inputs['utilobject'])
    pyfos_util.response_print(result)
    pyfos_auth.logout(session)
Ejemplo n.º 8
0
def main(argv):
    filters = ["dns_servers_dns_server", "domain_name"]
    inputs = brcd_util.parse(argv, fibrechannel_switch, filters, validate)
    swobject = inputs['utilobject']
    session = brcd_util.getsession(inputs)
    if session['version'] < version.fosversion("8.2.1"):
        skipattributes = ""
        if swobject.peek_domain_name() is not None:
            skipattributes += "domain-name"
        if swobject.peek_dns_servers_dns_server() != "[]":
            if len(skipattributes) > 0:
                skipattributes += ","
            skipattributes += "dns-servers/dns-server"
        if len(skipattributes) > 0:
            print("Skipping following attributes:(", skipattributes,
                  ") for patch as the switch fos version[",
                  session['version'].to_string(),
                  "]is below the supported attribute version of 8.2.1")
        pyfos_auth.logout(session)
        return
    result = _modify_dns_config_params(session, swobject)
    pyfos_util.response_print(result)
    pyfos_auth.logout(session)
Ejemplo n.º 9
0
def login(username,
          password,
          ip_addr,
          is_https,
          throttle_delay=DEFAULT_THROTTLE_DELAY,
          verbose=0):
    """Establish a session to the FOS switch based on a valid login credential.

    :param username: User name to establish a session.
    :param password: Password to establish a session.
    :param ip_addr: IP address of the FOS switch with which to \
            establish a session.
    :param is_https: None to indicate HTTP, "self" to indicate \
            HTTPS with a self-signed certificate, and "CA" to indicate \
            HTTPS with a CA-signed certificate.
    :param throttle_delay: Delay in seconds, to account for the maximum \
            number of requests allowed on FOS RESTCONF. The maximum number \
            of request per period is configurable on FOS.
    :param verbose: 1 for verbose session.
    :rtype: Dictionary of the error description.
        :ref:`Generic error structure <error-structure-reference>` \
                or session description.
    """
    credential = None

    try:
        credential, contentdict = pyfos_login.login(username, password,
                                                    ip_addr, is_https)
    except socket_error as serr:
        if serr.errno == errno.ECONNREFUSED:
            if throttle_delay > 0:
                time.sleep(throttle_delay)

            return {
                CREDENTIAL_KEY: {
                    LOGIN_ERROR_KEY: ip_addr + " refused connection"
                },
                "ip_addr": ip_addr,
                "vfid": -1,
                "ishttps": is_https,
                "debug": 0,
                "version": None,
                "throttle_delay": throttle_delay
            }
        elif serr.errno == errno.EHOSTUNREACH:
            if throttle_delay > 0:
                time.sleep(throttle_delay)

            return {
                CREDENTIAL_KEY: {
                    LOGIN_ERROR_KEY: ip_addr + " not reachable"
                },
                "ip_addr": ip_addr,
                "vfid": -1,
                "ishttps": is_https,
                "debug": 0,
                "version": None,
                "throttle_delay": throttle_delay
            }
        else:
            if throttle_delay > 0:
                time.sleep(throttle_delay)

            return {
                CREDENTIAL_KEY: {
                    LOGIN_ERROR_KEY:
                    ip_addr + " unknown error: " + str(serr.errno)
                },
                "ip_addr": ip_addr,
                "vfid": -1,
                "ishttps": is_https,
                "debug": 0,
                "version": None,
                "throttle_delay": throttle_delay
            }

    if isinstance(credential, dict) and LOGIN_ERROR_KEY in credential.keys():
        if throttle_delay > 0:
            time.sleep(throttle_delay)

        return {
            CREDENTIAL_KEY: {
                LOGIN_ERROR_KEY: ip_addr + " " + credential[LOGIN_ERROR_KEY]
            },
            "ip_addr": ip_addr,
            "vfid": -1,
            "ishttps": is_https,
            "debug": 0,
            "version": None,
            "throttle_delay": throttle_delay
        }

    if isinstance(credential, list):
        if throttle_delay > 0:
            time.sleep(throttle_delay)

        return credential
    else:
        # default version
        defversion = fosversion("8.2.1b")
        defcredver = fosversion("1.3.0")
        credver = None
        session = {
            CREDENTIAL_KEY: credential,
            "ip_addr": ip_addr,
            "vfid": -1,
            "ishttps": is_https,
            "debug": 0,
            "version": None,
            "throttle_delay": throttle_delay
        }
        session.update(contentdict)
        if contentdict['content-version'] is not None:
            credverstr = str(contentdict['content-version'])
            credverlist = credverstr.split("=")
            credver = fosversion(credverlist[1])

        if credver is not None and credver >= defcredver:
            session["version"] = defversion

        switchobj = pyfos_switch.fibrechannel_switch.get(session)
        if isinstance(switchobj, pyfos_switch.fibrechannel_switch):
            fwversion = fosversion(switchobj.peek_firmware_version())
            if fwversion == defversion and fwversion.release == "*":
                fwversion = defversion
            try:
                fwversion.to_string()
            except AttributeError as error:
                switchobj.dbg_print(4, "Incorrect FOS version retrieved :",
                                    switchobj.peek_firmware_version(), error)
                logout(session)
                return {
                    CREDENTIAL_KEY: {
                        LOGIN_ERROR_KEY:
                        ip_addr + " Incorrect FOS version string :" +
                        switchobj.peek_firmware_version()
                    },
                    "ip_addr": ip_addr,
                    "vfid": -1,
                    "ishttps": is_https,
                    "debug": 0,
                    "version": switchobj.peek_firmware_version(),
                    "throttle_delay": throttle_delay
                }
            session["version"] = fwversion

        if throttle_delay > 0:
            time.sleep(throttle_delay)

        debug_set(session, verbose)

        session["username"] = username
        session["password"] = password

        return session
Ejemplo n.º 10
0
def login(username,
          password,
          ip_addr,
          is_https,
          throttle_delay=DEFAULT_THROTTLE_DELAY,
          verbose=0,
          tokenArg=None,
          sessionless=False,
          nocred=False):
    """Establish a session to the FOS switch based on a valid login credential.

    :param username: User name to establish a session.
    :param password: Password to establish a session.
    :param ip_addr: IP address of the FOS switch with which to \
            establish a session.
    :param is_https: None to indicate HTTP, "self" to indicate \
            HTTPS with a self-signed certificate, and "CA" to indicate \
            HTTPS with a CA-signed certificate.
    :param throttle_delay: Delay in seconds, to account for the maximum \
            number of requests allowed on FOS RESTCONF. The maximum number \
            of request per period is configurable on FOS.
    :param verbose: 1 for verbose session.
    :param tokenArg: The "AuthToken" input argument, The values supported are \
            a string "AuthToken" or an "AuthTokenManager" instance. The utils \
            will use TokenManager instance internally for its use. The custom \
            tokenManager implementation may pass the string "AuthToken".
    :param sessionless: The session-less authentication for REST request. \
            The value passed is False by default. If the value is True then \
            login session is not used, however existing infra can continue to \
            use the login and logout infra as no-operation in this scenario .
    :param nocred: There is no credential needed for the REST request. \
            The support is limited to some known requests only and is not\
            supported for all REST requests.\
    :rtype: Dictionary of the error description.
        :ref:`Generic error structure <error-structure-reference>` \
                or session description.
    """
    if nocred is True:
        return {
            CREDENTIAL_KEY: None,
            "user": None,
            "ip_addr": ip_addr,
            "vfid": -1,
            "ishttps": is_https,
            "debug": 0,
            "throttle_delay": throttle_delay,
            "auth_token": tokenArg,
            "sessionless": sessionless,
            "nocredential": nocred,
            "version": None
        }
    credential = None
    authtoken = None
    if tokenArg is not None:
        if password is not None:
            # print("Error: password not needed for authtoken based session.")
            return {
                CREDENTIAL_KEY: {
                    LOGIN_ERROR_KEY:
                    ip_addr + " Password not needed for "
                    "authtoken based REST session."
                },
                "user": username,
                "ip_addr": ip_addr,
                "vfid": -1,
                "ishttps": is_https,
                "debug": 0,
                "version": None,
                "throttle_delay": throttle_delay,
                "auth_token": tokenArg,
                "sessionless": sessionless
            }
        else:
            if isinstance(tokenArg, auth_token_manager):
                authtoken = tokenArg.findToken(ip_addr, username)
                if authtoken is None or authtoken.auth_token is None:
                    return {
                        CREDENTIAL_KEY: {
                            LOGIN_ERROR_KEY: ip_addr + " No Auth Token"
                        },
                        "user": username,
                        "ip_addr": ip_addr,
                        "vfid": -1,
                        "ishttps": is_https,
                        "debug": 0,
                        "version": None,
                        "throttle_delay": throttle_delay,
                        "auth_token": tokenArg,
                        "sessionless": sessionless
                    }
            elif isinstance(tokenArg, str):
                authtoken = auth_token()
                authtoken.setValue(ip_addr, username, tokenArg)

    try:
        credential, contentdict = pyfos_login.login(username, password,
                                                    ip_addr, is_https,
                                                    throttle_delay, authtoken,
                                                    sessionless)
    except socket_error as serr:
        if serr.errno == errno.ECONNREFUSED:
            return {
                CREDENTIAL_KEY: {
                    LOGIN_ERROR_KEY: ip_addr + " refused connection"
                },
                "user": username,
                "ip_addr": ip_addr,
                "vfid": -1,
                "ishttps": is_https,
                "debug": 0,
                "version": None,
                "throttle_delay": throttle_delay,
                "auth_token": tokenArg,
                "sessionless": sessionless
            }
        elif serr.errno == errno.EHOSTUNREACH:
            return {
                CREDENTIAL_KEY: {
                    LOGIN_ERROR_KEY: ip_addr + " not reachable"
                },
                "user": username,
                "ip_addr": ip_addr,
                "vfid": -1,
                "ishttps": is_https,
                "debug": 0,
                "version": None,
                "throttle_delay": throttle_delay,
                "auth_token": tokenArg,
                "sessionless": sessionless
            }
        else:
            return {
                CREDENTIAL_KEY: {
                    LOGIN_ERROR_KEY:
                    ip_addr + " unknown error: " + str(serr.errno)
                },
                "user": username,
                "ip_addr": ip_addr,
                "vfid": -1,
                "ishttps": is_https,
                "debug": 0,
                "version": None,
                "throttle_delay": throttle_delay,
                "auth_token": tokenArg,
                "sessionless": sessionless
            }

    if isinstance(credential, dict) and LOGIN_ERROR_KEY in credential.keys():
        return {
            CREDENTIAL_KEY: {
                LOGIN_ERROR_KEY: ip_addr + " " + credential[LOGIN_ERROR_KEY]
            },
            "ip_addr": ip_addr,
            "user": username,
            "vfid": -1,
            "ishttps": is_https,
            "debug": 0,
            "version": None,
            "throttle_delay": throttle_delay,
            "auth_token": tokenArg,
            "sessionless": sessionless
        }

    if isinstance(credential, list):
        return credential
    else:
        # default version
        defversion = fosversion("8.2.1b")
        defcredver = fosversion("1.3.0")
        credver = None
        session = {
            CREDENTIAL_KEY: credential,
            "ip_addr": ip_addr,
            "user": username,
            "vfid": -1,
            "ishttps": is_https,
            "debug": 0,
            "version": None,
            "throttle_delay": throttle_delay,
            "auth_token": tokenArg,
            "sessionless": sessionless
        }
        session.update(contentdict)
        if contentdict['content-version'] is not None:
            credverstr = str(contentdict['content-version'])
            credverlist = credverstr.split("=")
            credver = fosversion(credverlist[1])

        if credver is not None and credver >= defcredver:
            session["version"] = defversion

        switchobj = pyfos_switch.fibrechannel_switch.get(session)
        if isinstance(switchobj, pyfos_switch.fibrechannel_switch):
            fwversion = fosversion(switchobj.peek_firmware_version())
            if fwversion == defversion and fwversion.release == "*":
                fwversion = defversion
            try:
                fwversion.to_string()
            except AttributeError as error:
                switchobj.dbg_print(4, "Incorrect FOS version retrieved :",
                                    switchobj.peek_firmware_version(), error)
                logout(session)
                return {
                    CREDENTIAL_KEY: {
                        LOGIN_ERROR_KEY:
                        ip_addr + " Incorrect FOS version string :" +
                        switchobj.peek_firmware_version()
                    },
                    "ip_addr": ip_addr,
                    "user": username,
                    "vfid": -1,
                    "ishttps": is_https,
                    "debug": 0,
                    "version": switchobj.peek_firmware_version(),
                    "throttle_delay": throttle_delay,
                    "auth_token": tokenArg,
                    "sessionless": sessionless
                }
            session["version"] = fwversion

        debug_set(session, verbose)

        session["username"] = username
        session["password"] = password

        return session