Пример #1
0
def nix_conn_generic(transport):
    if transport == "telnet":
        pytest.skip("skipping telnet for linux hosts")

    device = DEVICES["linux"].copy()
    device.pop("driver")

    conn = GenericDriver(**device, transport=transport,)
    conn.open()
    return conn
Пример #2
0
def main():
    """Simple example of connecting to an IOSXEDevice with the GenericDriver"""
    # the `GenericDriver` is a good place to start if your platform is not supported by a "core"
    #  platform drivers
    conn = GenericDriver(**MY_DEVICE)
    conn.open()

    print(conn.channel.get_prompt())
    print(conn.send_command("show run | i hostname").result)

    # IMPORTANT: paging is NOT disabled w/ GenericDriver driver!
    conn.send_command("terminal length 0")
    print(conn.send_command("show run").result)
    conn.close()

    # Context manager is a great way to use scrapli, it will auto open/close the connection for you:
    with GenericDriver(**MY_DEVICE) as conn:
        result = conn.send_command("show run | i hostname")
    print(result.result)
Пример #3
0
def main():
    """Example of working with a non "core" or non standard device"""
    # using a cisco WLC as since it has an interesting login pattern where the it prompts for the
    #  username after the initial ssh connection (even though ssh already knows your username!)
    wlc = {
        "host": "1.2.3.4",
        "auth_username": "******",
        "auth_password": "******",
        "auth_strict_key": False,
        "auth_bypass": True,
        # set a custom "on_open" function to deal with the non-standard login
        "on_open": wlc_on_open,
        # set a custom "comms_prompt_pattern" to deal with the non-standard prompt pattern
        "comms_prompt_pattern": r"^\(Cisco Controller\) >$",
    }

    conn = GenericDriver(**wlc)
    conn.open()
    print(conn.get_prompt())
    print(conn.send_command("show boot").result)
    conn.close()
Пример #4
0
from scrapli.driver import GenericDriver

args = {
    "host": "172.18.0.11",
    "auth_username": "******",
    "auth_password": "******",
    "auth_strict_key": False,
}

# the `GenericDriver` is a good place to start if your platform is not supported by a "core" platform driver
conn = GenericDriver(**args)
conn.open()

print(conn.channel.get_prompt())
print(conn.send_command("show run | i hostname").result)

# paging is NOT disabled w/ GenericDriver driver!
conn.send_command("terminal length 0")
print(conn.send_command("show run").result)
conn.close()

# Context manager is a great way to use scrapli:
with GenericDriver(**args) as conn:
    result = conn.send_command("show run | i hostname")
print(result.result)
Пример #5
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
            extras: extras dict from connection_options for scrapli -- pass all other scrapli
                arguments here
            configuration: nornir configuration

        Returns:
            N/A  # noqa: DAR202

        Raises:
            NornirScrapliInvalidPlatform: if no platform or an invalid scrapli/napalm platform
                string is provided

        """
        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 22,
            "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)

        if not platform:
            raise NornirScrapliInvalidPlatform(
                f"No `platform` provided in inventory for host `{hostname}`"
            )

        if platform in PLATFORM_MAP:
            platform = PLATFORM_MAP.get(platform)

        if platform == "generic":
            connection = GenericDriver(**parameters)
        else:
            try:
                connection = Scrapli(**parameters, platform=platform)  # type: ignore
            except ModuleNotFoundError as exc:
                raise NornirScrapliInvalidPlatform(
                    f"Provided platform `{platform}` is not a valid scrapli or napalm platform, "
                    "or is not a valid scrapli-community platform."
                ) from exc

        connection.open()
        self.connection = connection  # pylint: disable=W0201
Пример #6
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
            extras: extras dict from connection_options for scrapli -- pass all other scrapli
                arguments here
            configuration: nornir configuration

        Returns:
            N/A  # noqa: DAR202

        Raises:
            NornirScrapliInvalidPlatform: if no platform or an invalid scrapli/napalm platform
                string is provided

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

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

        parameters.update(extras)

        if not platform:
            raise NornirScrapliInvalidPlatform(
                f"No `platform` provided in inventory for host `{hostname}`")

        if platform in PLATFORM_MAP:
            platform = PLATFORM_MAP.get(platform)

        if platform == "generic":
            connection = GenericDriver(**parameters)
        else:
            try:
                connection = Scrapli(**parameters,
                                     platform=platform)  # type: ignore
            except ModuleNotFoundError as exc:
                raise NornirScrapliInvalidPlatform(
                    f"Provided platform `{platform}` is not a valid scrapli or napalm platform, "
                    "or is not a valid scrapli-community platform.") from exc

        connection.open()
        self.connection = connection  # pylint: disable=W0201