File: prepare_devices.py

package info (click to toggle)
python-scrapli 2023.7.30-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,536 kB
  • sloc: python: 14,459; makefile: 72
file content (68 lines) | stat: -rw-r--r-- 2,658 bytes parent folder | download | duplicates (3)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys

import jinja2
from devices import DEVICES
from scrapli_cfg import ScrapliCfg

from scrapli import Scrapli

# we can just search at / because the "base_config" is already fully qualified
JINJA_LOADER = jinja2.FileSystemLoader(searchpath="/")
JINJA_ENV = jinja2.Environment(
    loader=JINJA_LOADER, trim_blocks=True, undefined=jinja2.StrictUndefined
)


def prepare_device(test_devices):
    # push base config via scrapli-cfg to ensure consistent testing experience
    for device in test_devices:
        base_config = DEVICES[device]["base_config"]
        conn_dict = {
            "host": DEVICES[device]["host"],
            "port": DEVICES[device]["port"],
            "auth_username": DEVICES[device]["auth_username"],
            "auth_password": DEVICES[device]["auth_password"],
            "auth_secondary": DEVICES[device]["auth_secondary"],
            "auth_strict_key": DEVICES[device]["auth_strict_key"],
            "transport_options": {
                "open_cmd": [
                    "-o",
                    "KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1",
                    "-o",
                    "PubkeyAcceptedKeyTypes=+ssh-rsa",
                    "-o",
                    "HostKeyAlgorithms=+ssh-dss,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ssh-ed25519",
                ]
            },
            "platform": device,
            # nxos on macos w/out acceleration is... slooooooooooow
            "timeout_ops": 120,
        }

        with Scrapli(**conn_dict) as conn:
            if device == "cisco_iosxe":
                # getting existing crypto "stuff" from the device to stuff it into config template
                # to avoid netconf complaints -- replacing crypto was strings caused things to not
                # get registered in the keychain and netconf-yang connections would get denied
                crypto_pki = conn.send_command(command="show run | section crypto")
                template = JINJA_ENV.get_template(f"{base_config}.j2")
                loaded_base_config = template.render(crypto_pki=crypto_pki.result)
            else:
                with open(base_config, "r") as f:
                    loaded_base_config = f.read()

            with ScrapliCfg(conn=conn) as cfg_conn:
                cfg_conn.load_config(config=loaded_base_config, replace=True)
                cfg_conn.commit_config()

                if device == "cisco_iosxe":
                    conn.send_config(config="no file prompt quiet")


def main():
    test_devices = sys.argv[1].split(",")
    prepare_device(test_devices)


if __name__ == "__main__":
    main()