File: sync_read_callback.py

package info (click to toggle)
python-scrapli 2023.7.30-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,512 kB
  • sloc: python: 14,451; makefile: 72
file content (46 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""examples.read_callback.sync_read_callback"""
from scrapli.driver.core import IOSXEDriver
from scrapli.driver.generic.base_driver import ReadCallback
from scrapli.driver.generic.sync_driver import GenericDriver

device = {
    "host": "c3560",
    "auth_strict_key": False,
    "ssh_config_file": True,
}


def callback_one(cls: GenericDriver, read_output: str):
    """Callback that enters config mode (as a silly example)"""
    _ = read_output

    # note that because cls is typed `GenericDriver` mypy/IDE will not like this, but it does work
    # because yay python :) (assuming the driver you use is a NetworkDriver of course)
    cls.acquire_priv("configuration")
    cls.channel.send_return()


def callback_two(cls: GenericDriver, read_output: str):
    """Callback that enters runs a silly command"""
    print(f"previous read output : {read_output}")

    r = cls.send_command("do show run | i hostname")
    print(f"result: {r.result}")


if __name__ == "__main__":
    with IOSXEDriver(**device) as conn:
        callbacks = [
            ReadCallback(
                contains="rtr1#",
                callback=callback_one,
                name="enter config mode callback",
                case_insensitive=False,
            ),
            ReadCallback(
                contains_re=r"^rtr1\(config\)#",
                callback=callback_two,
                complete=True,
            ),
        ]
        conn.read_callback(callbacks=callbacks, initial_input="show run | i hostname")