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
def main(): """ Example demonstrating handling authentication with ssh private key where the latter is protected with a password Make sure the key permissions are 0600! """ with GenericDriver(**MY_DEVICE) as conn: result = conn.send_command("show run | i hostname") print(result.result)
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()
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)
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)
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
def main_task(task): """ Main Task """ print(f'working on : {task.host.name}\n') device = { "host": task.host.hostname, "auth_username": task.host.username, "auth_password": task.host.password, "auth_strict_key": False, "transport": "paramiko", "port": 830, "timeout_socket": 2 } with GenericDriver(**device) as conn: # conn.send_command('yp-shell') prompt = conn.get_prompt() if "@NFV-FRU>" not in prompt: conn.close() return " 10x system .. check manually" # PRE-CHECKS command = 'sget /ntp-state' res = conn.send_command(command) if '"synchronized":true' in res.result: conn.close() return "Already in SYNC" else: # CHANGE : ADD NTP command = 'config t' conn.send_command(command) command = 'sys:system ciena-ntp:ntp admin-state enabled' res = conn.send_command(command) if "failed" in res.result: conn.close() return " Error enabling NTP ... inspect manually" serv = "216.239.35.0" srv_added = False command = 'sys:system ciena-ntp:ntp associations remote-ntp-server server-entry 216.239.35.0 admin-state enabled' res = conn.send_command(command) # cover case where we don't have "applied" in output and where we do have "applied" but we also have failed if 'Applying 1 edit' not in res.result or "failed" in res.result: print(f'{task.host.name}: Error applying config for {serv}') else: srv_added = True serv = "37.187.5.167" command = 'sys:system ciena-ntp:ntp associations remote-ntp-server server-entry 37.187.5.167 admin-state enabled' res = conn.send_command(command) conn.send_command('exit') if 'Applying 1 edit' not in res.result or "failed" in res.result: print(f'{task.host.name}: Error applying config for {serv}') else: srv_added = True if srv_added: conn.close() return " Error adding servers " # VALIDATE NTP settings i = 3 for j in range(i): time.sleep( 5 ) # wait 5 seconds for dnfvi to contact NTP server to have server-state : reach command = 'sget /ntp-state' res = conn.send_command(command) if '"server-state":"reach"' in res.result: time.sleep(5) # wait 5 more seconds to have them in sync res = conn.send_command(command) if '"synchronized":true' in res.result: return "NTP in SYNC" return "Added servers not reachable or in sync after 30 sec"
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