Exemple #1
0
    def open(
        self,
        hostname: Optional[str],
        username: Optional[str],
        password: Optional[str],
        port: Optional[int],
        platform: Optional[str],
        extras: Optional[Dict[str, Any]] = None,
        configuration: Optional[Config] = None,
    ) -> None:
        """
        Open a scrapli connection to a device

        Args:
            hostname: hostname from nornir inventory
            username: username from nornir inventory/connection_options for scrapli
            password: password from nornir inventory/connection_options for scrapli
            port: port from nornir inventory/connection_options for scrapli
            platform: platform from nornir inventory/connection_options for scrapli; ignored with
                scrapli netconf
            extras: extras dict from connection_options for scrapli -- pass all other scrapli
                arguments here
            configuration: nornir configuration

        Returns:
            N/A  # noqa: DAR202

        Raises:
            N/A

        """
        # platform is irrelevant for scrapli netconf for now
        _ = platform
        extras = extras or {}
        # 99.9% configuration will always be passed here... but to be consistent w/ the other
        # plugins we'll leave the function signature same/same as the others
        global_config = configuration.dict() if configuration else {}

        parameters: Dict[str, Any] = {
            "host": hostname,
            "auth_username": username or "",
            "auth_password": password or "",
            "port": port or 830,
            "ssh_config_file": global_config.get("ssh", {}).get("config_file", False),
        }

        # will override any of the configs from global nornir config (such as ssh config file) with
        # options from "extras" (connection options)
        parameters.update(extras)

        connection = NetconfScrape(**parameters)
        connection.open()
        self.connection = connection  # pylint: disable=W0201
Exemple #2
0
def main():
    """Basic use example"""
    # create scrapli_netconf connection just like with scrapli, open the connection
    conn = NetconfScrape(**JUNOS_DEVICE)
    conn.open()

    # lock the candidate config before starting because why not
    result = conn.lock(target="candidate")
    print(result.result)

    # get the whole config; just like scrapli the result is a `Response` object, in this case a
    # `NetconfResponse` object with some additional methods
    result = conn.get_config()
    # print xml text result
    print(result.result)
    # print xml element result
    print(result.xml_result)

    # get the whole config, but apply some filters (subtree filters) in the case of junos since its
    # just a config, not a model and we are filtering for things under `configuration` this has to
    # live in a single filter unlike iosxr
    result = conn.get_config(filters=CONFIG_FILTER)
    print(result.result)

    # get some operational data via "rpc" for juniper style rpc calls; note the `filter_` to
    # not reuse builtins
    result = conn.rpc(filter_=COMMIT_FILTER)
    print(result.result)

    # edit the candidate configuration
    result = conn.edit_config(config=EDIT_NETCONF, target="candidate")
    print(result.result)

    # commit config changes
    conn.commit()
    print(result.result)

    # edit multiple config elements
    result = conn.edit_config(config=EDIT_MULTIPLE, target="candidate")
    print(result.result)

    # discard this config change
    result = conn.discard()
    print(result.result)

    # unlock the candidate now that we're done
    result = conn.unlock(target="candidate")
    print(result.result)

    # close the session
    conn.close()
def main():
    # create scrapli_netconf connection just like with scrapli, open the connection
    conn = NetconfScrape(**IOSXR_DEVICE)
    conn.open()

    # lock the candidate config before starting because why not
    result = conn.lock(target="candidate")
    print(result.result)

    # get the whole config; just like scrapli the result is a `Response` object, in this case a
    # `NetconfResponse` object with some additional methods
    result = conn.get_config()
    # print xml text result
    print(result.result)
    # print xml element result
    print(result.xml_result)

    # get the whole config, but apply some filters (subtree filters)
    filters = [INTERFACE_ACTIVE_FILTER, NETCONF_YANG_FILTER]
    result = conn.get_config(filters=filters)
    print(result.result)

    # get something other than the config; note the `filter_` to not reuse builtins
    result = conn.get(filter_=PLATFORM_FILTER)
    print(result.result)

    # edit the candidate configuration
    result = conn.edit_config(config=EDIT_CDP, target="candidate")
    print(result.result)

    # commit config changes
    conn.commit()
    print(result.result)

    # stage a config we'll discard
    config = EDIT_BANNER
    result = conn.edit_config(config=config, target="candidate")
    print(result.result)

    # discard this config change
    result = conn.discard()
    print(result.result)

    # unlock the candidate now that we're done
    result = conn.unlock(target="candidate")
    print(result.result)

    # close the session
    conn.close()
    def open(
        self,
        hostname: Optional[str],
        username: Optional[str],
        password: Optional[str],
        port: Optional[int],
        platform: Optional[str],
        extras: Optional[Dict[str, Any]] = None,
        configuration: Optional[Config] = None,
    ) -> None:
        """
        Open a scrapli connection to a device

        Args:
            hostname: hostname from nornir inventory
            username: username from nornir inventory/connection_options for scrapli
            password: password from nornir inventory/connection_options for scrapli
            port: port from nornir inventory/connection_options for scrapli
            platform: platform from nornir inventory/connection_options for scrapli; ignored with
                scrapli netconf
            extras: extras dict from connection_options for scrapli -- pass all other scrapli
                arguments here
            configuration: nornir configuration

        Returns:
            N/A  # noqa: DAR202

        Raises:
            N/A

        """
        # for now not trying to get ssh config out of configuration, but should in the future...
        # platform is irrelevant for scrapli netconf for now
        _, _ = configuration, platform
        extras = extras or {}

        parameters: Dict[str, Any] = {
            "host": hostname,
            "auth_username": username or "",
            "auth_password": password or "",
            "port": port or 22,
        }

        parameters.update(extras)

        connection = NetconfScrape(**parameters)
        connection.open()
        self.connection = connection  # pylint: disable=W0201
Exemple #5
0
def main():
    # create scrapli_netconf connection just like with scrapli, open the connection
    conn = NetconfScrape(**IOSXR_DEVICE)
    conn.open()

    # lock the candidate config before starting because why not
    result = conn.lock(target="candidate")
    print(result.result)

    config = EDIT_INTERFACE_G_0_0_0_0
    result = conn.edit_config(config=config, target="candidate")
    print(result.result)

    # commit config changes
    conn.commit()
    print(result.result)

    # unlock the candidate now that we're done
    result = conn.unlock(target="candidate")
    print(result.result)

    # close the session
    conn.close()
Exemple #6
0
from scrapli_netconf.driver import NetconfScrape

my_device = {
    "host": "sandbox-iosxe-latest-1.cisco.com",
    "auth_username": "******",
    "auth_password": "******",
    "auth_strict_key": False,
    "port": 830
}

conn = NetconfScrape(**my_device)
conn.open()

ospf_xpath = '/ospf-oper-data/ospf-state/ospf-instance[af="address-family-ipv4" and router-id="235802126"]/ospf-area[area-id=599]/ospf-interface[name="Loopback14"]'
response = conn.get(filter_=ospf_xpath, filter_type='xpath')
print(response.result)