def nc_get_edit_cfg(device_data: Dict[str, Any], cfg: str):
    """Retrieves config with get-config and changes it with edit-config with the
    input from YAML file using scrapli-netconf"""
    dt_str = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
    device_name = device_data["device_name"]
    output_path = OUTPUT_DIR / f"{device_name}_{dt_str}_config.xml"
    conn_data = create_conn_data(device_data)
    with NetconfDriver(**conn_data) as nc_conn, open(output_path, "wb") as f:
        nc_conn = cast(NetconfDriver, nc_conn)
        nc_response = nc_conn.get_config(source="running")
        xml_bytes = etree.tostring(nc_response.xml_result, pretty_print=True)
        f.write(xml_bytes)

        nc_response = nc_conn.edit_config(cfg, target="running")
        if nc_response.failed:
            raise ValueError(f"{device_name}: {nc_response.result}")
Пример #2
0
def test_init():
    conn = NetconfDriver(host="localhost")
    assert isinstance(conn.transport, NetconfSystemTransport)
def test_iosxr_commit_confirmed_timeout():
    print("""
# --------------------------------------------------
# Test commit confirmed with timeout
# --------------------------------------------------
    """)
    description = f"Configured by scrapli-netconf, random int - {random.randrange(1, 10000)}"
    iosxr1_conn = NetconfDriver(**iosxr1)
    iosxr1_conn.open()
    print("IOSXR OPEN")

    result = iosxr1_conn.edit_config(
        config=IOS_XR_EDIT_CONFIG.format(description), target="candidate")
    print("IOSXR EDIT: ", result.result)

    iosxr1_conn.commit(confirmed=True, timeout=10)
    print("IOSXR COMMIT CONFIRMED TIMEOUT 10s: ", result.result)

    print(f"IOSXR CHECK description is '{description}'")
    result = iosxr1_conn.get_config(source="running", filter_=IOS_XR_FILTER)
    print("IOSXR INTERFACE CONFIG: ", result.result)

    iosxr1_conn.close()
    print("IOSXR CLOSED")

    print("IOSXR sleeping 15 seconds for commit to timeout")
    time.sleep(15)

    iosxr1_conn = NetconfDriver(**iosxr1)
    iosxr1_conn.open()
    print("IOSXR OPEN AGAIN")

    print(f"IOSXR CHECK description is not '{description}'")
    result = iosxr1_conn.get_config(source="running", filter_=IOS_XR_FILTER)
    print("IOSXR INTERFACE CONFIG after timeout: ", result.result)

    iosxr1_conn.close()
    print("IOSXR CLOSED")
def test_iosxr_commit_confirmed_in_another_session():
    print("""
# --------------------------------------------------
# Test commit confirmed within another session
# using persist-id
# --------------------------------------------------
    """)
    description = f"Configured by scrapli-netconf, random int - {random.randrange(1, 10000)}"
    iosxr1_conn = NetconfDriver(**iosxr1)
    iosxr1_conn.open()
    print("IOSXR OPEN")

    result = iosxr1_conn.edit_config(
        config=IOS_XR_EDIT_CONFIG.format(description), target="candidate")
    print("IOSXR EDIT: ", result.result)

    iosxr1_conn.commit(confirmed=True, persist="foobar1234")
    print("IOSXR COMMIT CONFIRMED WITH PERSIST: ", result.result)

    iosxr1_conn.close()
    print("IOSXR CLOSED")

    iosxr1_conn = NetconfDriver(**iosxr1)
    iosxr1_conn.open()
    print("IOSXR OPEN AGAIN")

    iosxr1_conn.commit(persist_id="foobar1234")
    print("IOSXR COMMIT PERSIST-ID: ", result.result)

    print(f"IOSXR CHECK description is '{description}'")
    result = iosxr1_conn.get_config(source="running", filter_=IOS_XR_FILTER)
    print("IOSXR INTERFACE CONFIG", result.result)

    iosxr1_conn.close()
    print("IOSXR CLOSED")
def test_iosxr_commit_confirmed_in_same_session():
    print("""
# --------------------------------------------------
# Test commit confirmed within same session
# --------------------------------------------------
    """)
    description = f"Configured by scrapli-netconf, random int - {random.randrange(1, 10000)}"
    iosxr1_conn = NetconfDriver(**iosxr1)
    iosxr1_conn.open()
    print("IOSXR OPEN")

    result = iosxr1_conn.lock(target="candidate")
    print("IOSXR LOCK: ", result.result)

    result = iosxr1_conn.edit_config(
        config=IOS_XR_EDIT_CONFIG.format(description), target="candidate")
    print("IOSXR EDIT: ", result.result)

    iosxr1_conn.commit(confirmed=True)
    print("IOSXR COMMIT CONFIRMED: ", result.result)

    iosxr1_conn.commit()
    print("IOSXR COMMIT: ", result.result)

    result = iosxr1_conn.unlock(target="candidate")
    print("IOSXR UNLOCK: ", result.result)

    print(f"IOSXR CHECK description is '{description}'")
    result = iosxr1_conn.get_config(source="running", filter_=IOS_XR_FILTER)
    print("IOSXR INTERFACE CONFIG", result.result)

    iosxr1_conn.close()
    print("IOSXR CLOSED")
Пример #6
0
def dummy_conn():
    conn = NetconfDriver(host="localhost")
    return conn
Пример #7
0
def test_init():
    conn = NetconfDriver(host="localhost", transport="ssh2")
    assert isinstance(conn.transport, NetconfSsh2Transport)
Пример #8
0
def test_init():
    # importing in here for now to not break 3.10 tests
    from scrapli_netconf.transport.plugins.ssh2.transport import NetconfSsh2Transport

    conn = NetconfDriver(host="localhost", transport="ssh2")
    assert isinstance(conn.transport, NetconfSsh2Transport)
Пример #9
0
def test_init():
    conn = NetconfDriver(host="localhost", transport="paramiko")
    assert isinstance(conn.transport, NetconfParamikoTransport)