File: helper.py

package info (click to toggle)
python-scrapli 2023.7.30-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,536 kB
  • sloc: python: 14,459; makefile: 72
file content (155 lines) | stat: -rw-r--r-- 6,243 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import re


def cisco_iosxe_clean_response(response):
    def _replace_config_bytes(response):
        config_bytes_pattern = re.compile(r"^Current configuration : \d+ bytes$", flags=re.M | re.I)
        response = re.sub(config_bytes_pattern, "Current configuration : CONFIG_BYTES", response)
        return response

    def _replace_timestamps(response):
        datetime_pattern = re.compile(
            r"\d+:\d+:\d+\d+\s+[a-z]{3}\s+(mon|tue|wed|thu|fri|sat|sun)\s+(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+\d+\s+\d+",
            flags=re.M | re.I,
        )
        response = re.sub(datetime_pattern, "TIME_STAMP_REPLACED", response)
        return response

    def _replace_configured_by(response):
        configured_by_pattern = re.compile(
            r"^! Last configuration change at TIME_STAMP_REPLACED by (\w+)$", flags=re.M | re.I
        )
        response = re.sub(
            configured_by_pattern, "! Last configuration change at TIME_STAMP_REPLACED", response
        )
        return response

    def _replace_hashed_passwords(response):
        crypto_pattern = re.compile(r"^enable secret 5 (.*$)", flags=re.M | re.I)
        response = re.sub(crypto_pattern, "enable secret 5 HASHED_PASSWORD", response)
        return response

    def _replace_call_home_comment(response):
        # this shows up in some csr1000v versions, so strip it out just in case
        crypto_pattern = re.compile(
            r"(^.*$)\n^! Call-home is enabled by Smart-Licensing.$(\n^.*$)", flags=re.M | re.I
        )
        response = re.sub(crypto_pattern, r"\1\2", response)
        return response

    def _replace_certificates_and_license(response):
        # replace pki/certificate stuff and license all in one go
        crypto_pattern = re.compile(
            r"^crypto pki .*\nlicense udi pid CSR1000V sn \w+$", flags=re.M | re.I | re.S
        )
        response = re.sub(crypto_pattern, "CERTIFICATES AND LICENSE", response)
        return response

    response = _replace_config_bytes(response)
    response = _replace_timestamps(response)
    response = _replace_configured_by(response)
    response = _replace_hashed_passwords(response)
    response = _replace_call_home_comment(response)
    response = _replace_certificates_and_license(response)
    return response


def cisco_iosxr_clean_response(response):
    def _replace_timestamps(response):
        datetime_pattern = re.compile(
            r"(mon|tue|wed|thu|fri|sat|sun)\s+(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+\d+\s+\d+:\d+:\d+((\.\d+\s\w+)|\s\d+)",
            flags=re.M | re.I,
        )
        response = re.sub(datetime_pattern, "TIME_STAMP_REPLACED", response)
        return response

    def _replace_configured_by(response):
        configured_by_pattern = re.compile(
            r"^!! Last configuration change at TIME_STAMP_REPLACED by (\w+)$", flags=re.M | re.I
        )
        response = re.sub(
            configured_by_pattern, "!! Last configuration change at TIME_STAMP_REPLACED", response
        )
        return response

    def _replace_crypto_strings(response):
        crypto_pattern = re.compile(r"^\ssecret\s5\s[\w$\.\/]+$", flags=re.M | re.I)
        response = re.sub(crypto_pattern, "CRYPTO_REPLACED", response)
        return response

    def _replace_commit_in_progress(response):
        commit_in_progress_pattern = re.compile(r"System configuration.*", flags=re.M | re.I | re.S)
        response = re.sub(commit_in_progress_pattern, "", response)
        return response.rstrip()

    response = _replace_timestamps(response)
    response = _replace_configured_by(response)
    response = _replace_crypto_strings(response)
    response = _replace_commit_in_progress(response)
    return response


def cisco_nxos_clean_response(response):
    def _replace_timestamps(response):
        datetime_pattern = re.compile(
            r"(mon|tue|wed|thu|fri|sat|sun)\s+(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+\d+\s+\d+:\d+:\d+\s\d+",
            flags=re.M | re.I,
        )
        response = re.sub(datetime_pattern, "TIME_STAMP_REPLACED", response)
        return response

    def _replace_crypto_strings(response):
        crypto_pattern = re.compile(r"^(.*?\s(?:5|md5)\s)[\w$\.\/]+.*$", flags=re.M | re.I)
        response = re.sub(crypto_pattern, "CRYPTO_REPLACED", response)
        return response

    def _replace_resource_limits(response):
        crypto_pattern = re.compile(
            r"^(\s+limit-resource\s[a-z0-9\-]+\sminimum\s)\d+\smaximum\s\d+$", flags=re.M | re.I
        )
        response = re.sub(crypto_pattern, r"\1N maximum N", response)
        return response

    response = _replace_timestamps(response)
    response = _replace_crypto_strings(response)
    response = _replace_resource_limits(response)
    return response


def arista_eos_clean_response(response):
    def _replace_timestamps(response):
        datetime_pattern = re.compile(
            r"(mon|tue|wed|thu|fri|sat|sun)\s+(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+\d+\s+\d+:\d+:\d+\s+\d+$",
            flags=re.M | re.I,
        )
        response = re.sub(datetime_pattern, "TIME_STAMP_REPLACED", response)
        return response

    def _replace_crypto_strings(response):
        crypto_pattern = re.compile(r"secret\ssha512\s[\w$\.\/]+$", flags=re.M | re.I)
        response = re.sub(crypto_pattern, "CRYPTO_REPLACED", response)
        return response

    response = _replace_timestamps(response)
    response = _replace_crypto_strings(response)
    return response


def juniper_junos_clean_response(response):
    def _replace_timestamps(response):
        datetime_pattern = re.compile(
            r"^## Last commit: \d+-\d+-\d+\s\d+:\d+:\d+\s\w+.*$", flags=re.M | re.I
        )
        response = re.sub(datetime_pattern, "TIME_STAMP_REPLACED", response)
        return response

    def _replace_crypto_strings(response):
        crypto_pattern = re.compile(
            r'^\s+encrypted-password\s"[\w$\.\/]+";\s.*$', flags=re.M | re.I
        )
        response = re.sub(crypto_pattern, "CRYPTO_REPLACED", response)
        return response

    response = _replace_timestamps(response)
    response = _replace_crypto_strings(response)
    return response