def test_unsupported_platform(): original_platform = sys.platform sys.platform = "win32" with pytest.raises(UnsupportedPlatform) as exc: Scrape(host="localhost") assert ( str(exc.value) == "`system` transport is not supported on Windows, please use a different transport" ) sys.platform = original_platform
def test__repr(): conn = Scrape(host="myhost") assert ( repr(conn) == "Scrape(host='myhost', port=22, auth_username='', auth_password='', auth_private_key='', " "auth_private_key_passphrase='', auth_strict_key=True, auth_bypass=False, timeout_socket=10, " "timeout_transport=30, timeout_ops=30.0, timeout_exit=True, comms_prompt_pattern='^[a-z0-9.\\\\-@()/:]{1," "48}[#>$]\\\\s*$', comms_return_char='\\n', comms_ansi=False, ssh_config_file='', ssh_known_hosts_file='', " "on_init=None, on_open=None, on_close=None, transport='system', transport_options=None)" )
def test__repr(): conn = Scrape(host="myhost") assert ( repr(conn) == "Scrape(host='myhost', port=22, auth_username='', auth_password='', auth_private_key=b'', " "auth_strict_key=True, timeout_socket=5, timeout_transport=5, timeout_ops=10, timeout_exit=True, " "keepalive=False, keepalive_interval=30, keepalive_type='network', keepalive_pattern='\\x05', " "comms_prompt_pattern='^[a-z0-9.\\\\-@()/:]{1,32}[#>$]\\\\s*$', comms_return_char='\\n', comms_ansi=False, " "ssh_config_file='', ssh_known_hosts_file='', on_open=None, on_close=None, transport='system')" )
def test_exceptions_raised(attr_setup): attr_name = attr_setup[0] attr_value = attr_setup[1] attr_exc = attr_setup[2] attr_msg = attr_setup[3] args = {attr_name: attr_value} if attr_name != "host": args["host"] = "myhost" with pytest.raises(attr_exc) as exc: Scrape(**args) assert str(exc.value) == attr_msg
def test_ssh_files(fs, ssh_file): attr_name = ssh_file[0] attr_value = ssh_file[1] attr_expected = ssh_file[2] attr_src = ssh_file[3] args = {attr_name: attr_value} if attr_src and attr_expected: fs.add_real_file(source_path=attr_src, target_path=attr_expected) conn = Scrape(host="myhost", **args) assert conn._initialization_args[attr_name] == attr_expected
def test_attr_assignment(attr_setup): attr_name = attr_setup[0] attr_value = attr_setup[1] attr_expected = attr_setup[2] args = {attr_name: attr_value} if attr_name != "host": args["host"] = "myhost" conn = Scrape(**args) if attr_name == "transport": conn.transport_class == attr_expected else: assert conn._initialization_args.get(attr_name) == attr_expected
def main(): """Example demonstrating using the `Scrape` driver directly""" # the `Scrape` driver is only for use if you *really* want to manually handle the channel # input/output if your device type is supported by a core platform you should probably use that, # otherwise check out the `GenericDriver` before diving into `Scrape` as a last resort! conn = Scrape(**MY_DEVICE) conn.open() print(conn.channel.get_prompt()) print(conn.channel.send_input("show run | i hostname")[1]) # paging is NOT disabled w/ Scrape driver! conn.channel.send_input("terminal length 0") print(conn.channel.send_input("show run")[1]) conn.close() # Context manager is a great way to use scrapli: with Scrape(**MY_DEVICE) as conn: result = conn.channel.send_input("show run | i hostname") print(result[1])
def test_isalive_no_transport(): # test to ensure we handle attribute error to show that scrape is not alive if transport does # not exist yet conn = Scrape(host="myhost") assert conn.isalive() is False
def test_valid_private_key_file(): auth_private_key = f"{TEST_DATA_DIR}/files/_ssh_private_key" conn = Scrape(host="myhost", auth_private_key=auth_private_key) assert (conn._initialization_args["auth_private_key"] == f"{TEST_DATA_DIR}/files/_ssh_private_key")
def test_transport_options(): conn = Scrape(host="localhost", transport_options={"someoption": "somethingneat"}) assert conn.transport_args["transport_options"] == { "someoption": "somethingneat" }
def test_log_non_core_transport(caplog): with caplog.at_level(logging.INFO): Scrape(host="myhost", transport="paramiko") assert "Non-core transport `paramiko` selected" in caplog.text
def test__str(): conn = Scrape(host="myhost") assert str(conn) == "Scrape Object for host myhost"
import logging import time from pathlib import Path from device_info import iosxe_device from scrapli import Scrape logging.basicConfig( filename=f"{Path(__file__).resolve().parents[0]}/scrape_driver.log", level=logging.DEBUG ) logger = logging.getLogger("scrapli") conn = Scrape(**iosxe_device) conn.open() print("***** Get Prompt:") print(conn.channel.get_prompt()) print("***** Show run | i hostname:") result = conn.channel.send_input("show run | i hostname") print(result) print("***** Clear logging buffer:") interact = [("clear logg", "Clear logging buffer [confirm]"), ("", "csr1000v#")] result = conn.channel.send_inputs_interact(interact) print(result) print("***** Disable Paging:") result = conn.channel.send_input("term length 0") print(result)
from scrapli import Scrape args = { "host": "172.18.0.11", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, } # the `Scrape` driver is only for use if you *really* want to manually handle the channel input/output # if your device type is supported by a core platform you should probably use that, otherwise you should # check out the `GenericDriver` before diving into `Scrape` as a last resort! conn = Scrape(**args) conn.open() print(conn.channel.get_prompt()) print(conn.channel.send_input("show run | i hostname")[1]) # paging is NOT disabled w/ scrapli driver! conn.channel.send_input("terminal length 0") print(conn.channel.send_input("show run")[1]) conn.close() # Context manager is a great way to use scrapli: with Scrape(**args) as conn: result = conn.channel.send_input("show run | i hostname") print(result[1])
def test_valid_private_key_file(): auth_private_key = f"{UNIT_TEST_DIR}_ssh_private_key" conn = Scrape(host="myhost", auth_private_key=auth_private_key) assert conn._initialization_args["auth_private_key"] == f"{UNIT_TEST_DIR}_ssh_private_key"