1 | # py312 |
2 | # requirements: |
3 | # paramiko==5.1.0 |
4 | # pyeapi==1.0.4 |
5 |
|
6 | import base64 |
7 | import hashlib |
8 | import json |
9 | import logging |
10 | import re |
11 | import socket |
12 | import ssl |
13 | import time |
14 | from typing import TypedDict |
15 | from urllib.parse import urlsplit |
16 |
|
17 | import paramiko |
18 | import pyeapi |
19 |
|
20 |
|
21 | class arista_eos(TypedDict): |
22 | host: str |
23 | username: str |
24 | password: str |
25 | port: int |
26 | verify_tls: bool |
27 | allow_http: bool |
28 |
|
29 |
|
30 | class ReloadRefused(Exception): |
31 | pass |
32 |
|
33 |
|
34 | def main( |
35 | auth: arista_eos, |
36 | host: str = "", |
37 | allow_unsaved: bool = False, |
38 | skip_if_uptime_below_seconds: int = 0, |
39 | ): |
40 | """ |
41 | Reload |
42 | Reload the switch now (reload now: no save prompt). One attempt, no state: run it as a step that is not retried. Refuses with unsaved config (show running-config diffs) unless allow_unsaved. skip_if_uptime_below_seconds (0 = off) skips when the switch booted less than that ago, so a replayed step doesn't reload twice. Returns { reloaded, reason, reloaded_at, uptime_before_seconds, boot_timestamp_before }. Raises ReloadRefused, DeviceUnreachable or AuthFailure. Upgrade: Copy Image, Set Boot Image, Reload, then retry Get Facts on DeviceUnreachable until bootupTimestamp differs from boot_timestamp_before, and compare version; rollback: Set Boot Image with the previous_image Set Boot Image returned, then Reload. |
43 | """ |
44 | auth = with_host(auth, host) |
45 | eos = node(auth) |
46 | [version] = eos.run_commands(["show version"]) |
47 | before = { |
48 | "uptime_before_seconds": round(version["uptime"]), |
49 | "boot_timestamp_before": version["bootupTimestamp"], |
50 | } |
51 | if skip_if_uptime_below_seconds and version["uptime"] < skip_if_uptime_below_seconds: |
52 | return {"reloaded": False, "reason": f"rebooted {round(version['uptime'])} s ago", "reloaded_at": None, **before} |
53 | if not allow_unsaved and unsaved(eos): |
54 | raise ReloadRefused(f"{auth['host']}: running-config has unsaved changes: save it first, or set allow_unsaved") |
55 | reloaded_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) |
56 | # pyeapi opens the connection and sends in one call, so it is opened first: a failure there means nothing was |
57 | # sent. After that, the switch dropping the connection as it goes down is the expected outcome. |
58 | raw = open_node(auth, 15) |
59 | try: |
60 | raw.connection.transport.connect() |
61 | except OSError as e: |
62 | raise DeviceUnreachable(f"{auth['host']}: {e}") |
63 | try: |
64 | raw.run_commands(["reload now"]) |
65 | except pyeapi.eapilib.ConnectionError as e: |
66 | err = connection_error(auth, raw, e) |
67 | if isinstance(err, AuthFailure): |
68 | raise err |
69 | return {"reloaded": True, "reason": None, "reloaded_at": reloaded_at, **before} |
70 |
|
71 |
|
72 | # show running-config diffs is a unified diff of startup-config against running-config. Changed lines that start |
73 | # with a single "!" are headers EOS writes itself (boot system shows there); "!!" comments are config. With no |
74 | # startup-config every line shows as added. A refused command is ReloadRefused: saving wouldn't clear it. |
75 | def unsaved(eos): |
76 | try: |
77 | [d] = eos.run_commands(["show running-config diffs"], "text") |
78 | except pyeapi.eapilib.CommandError as e: |
79 | raise ReloadRefused(f"cannot compare running and startup config: {e}") |
80 | hunks, changed = False, [] |
81 | for line in d["output"].splitlines(): |
82 | hunks = hunks or line.startswith("@@") |
83 | if hunks and line[:1] in ("+", "-") and line[1:].strip(): |
84 | changed.append(line[1:].strip()) |
85 | return any(not re.match(r"!(?!!)", line) for line in changed) |
86 |
|
87 |
|
88 | # One resource can serve every switch that shares a login: the host input, when set, overrides the resource's host. |
89 | def with_host(auth: arista_eos, host: str): |
90 | host = (host or "").strip() or (auth.get("host") or "").strip() |
91 | if not host: |
92 | raise ValueError("host is required: set it on the resource or pass host") |
93 | return {**auth, "host": host} |
94 |
|
95 |
|
96 | # SSH transport (transport: ssh): the EOS CLI over an SSH shell, behind the part of pyeapi's Node the scripts use |
97 | # (run_commands, connection.socket_error, connection.transport.connect), raising pyeapi's own CommandError and |
98 | # ConnectionError, so the checks and the error mapping are the same for both transports. Show commands in json get |
99 | # "| json", which prints the eAPI JSON; other commands give { messages: [output] } like eAPI, or {} when silent. |
100 | # A line starting with "% " is an error, raised as eAPI's CommandError. The host key is checked when the resource |
101 | # has ssh_host_key (SHA256:... as ssh-keygen -lf prints it). |
102 | class Ssh: |
103 | def __init__(self, auth: arista_eos, timeout: float): |
104 | self.auth, self.timeout = auth, timeout |
105 | self.connection = self.transport = self |
106 | self.socket_error = self.sock = self.chan = None |
107 |
|
108 | # The TCP connection only, like eAPI's transport.connect(): if it fails, nothing was sent. |
109 | def connect(self): |
110 | self.sock = socket.create_connection(ssh_addr(self.auth), self.timeout) |
111 |
|
112 | def lost(self, e): |
113 | self.socket_error = e |
114 | return pyeapi.eapilib.ConnectionError("ssh", f"SSH connection failed ({e})") |
115 |
|
116 | def open(self): |
117 | auth = self.auth |
118 | try: |
119 | if not self.sock: |
120 | self.connect() |
121 | t = paramiko.Transport(self.sock) |
122 | t.start_client(timeout=self.timeout) |
123 | key = t.get_remote_server_key() |
124 | except (OSError, EOFError, paramiko.SSHException) as e: |
125 | raise self.lost(e) |
126 | fp = "SHA256:" + base64.b64encode(hashlib.sha256(key.asbytes()).digest()).decode().rstrip("=") |
127 | if auth.get("ssh_host_key") and auth["ssh_host_key"].strip() != fp: |
128 | t.close() |
129 | raise RuntimeError(f"{auth['host']}: the SSH host key is {fp}, not the resource's ssh_host_key") |
130 | try: |
131 | try: |
132 | t.auth_password(auth["username"], auth["password"]) |
133 | except paramiko.BadAuthenticationType: |
134 | t.auth_interactive( |
135 | auth["username"], lambda _t, _i, prompts: [auth["password"]] * len(prompts) |
136 | ) |
137 | except paramiko.AuthenticationException as e: |
138 | t.close() |
139 | raise pyeapi.eapilib.ConnectionError("ssh", str(e)) |
140 | except (OSError, EOFError, paramiko.SSHException) as e: |
141 | raise self.lost(e) |
142 | try: |
143 | self.chan = t.open_session() |
144 | self.chan.get_pty(width=4000) |
145 | self.chan.invoke_shell() |
146 | self.prompt = None |
147 | out = self.read() |
148 | self.prompt = re.escape(re.split(r"[(>#]", out.splitlines()[-1].strip())[0]) |
149 | if out.rstrip().endswith(">"): |
150 | self.cli("enable") |
151 | self.cli("terminal length 0") |
152 | self.cli("terminal width 32767") |
153 | except (OSError, EOFError, paramiko.SSHException) as e: |
154 | raise self.lost(e) |
155 |
|
156 | # Reads until the prompt. Questions eAPI never asks are answered as eAPI behaves: confirm, don't save. |
157 | def read(self): |
158 | buf, end = "", time.monotonic() + self.timeout |
159 | prompt = (self.prompt or r"[\w.\-]+") + r"(\([^)\r\n]*\))?[>#] ?$" |
160 | while True: |
161 | self.chan.settimeout(max(end - time.monotonic(), 0.01)) |
162 | chunk = self.chan.recv(65536) |
163 | if not chunk: |
164 | raise self.lost("connection closed") |
165 | buf += chunk.decode(errors="replace") |
166 | tail = buf[-300:] |
167 | if re.search(r"(^|\n)" + prompt, tail): |
168 | return buf |
169 | for question, answer in ( |
170 | # enable asks for a password on switches that land the user unprivileged: the login password. |
171 | (r"(^|\n)Password:\s*$", self.auth["password"]), |
172 | (r"\[confirm\]\s*$", ""), |
173 | (r"Save\?.*\[yes/no[^\]]*\]:?\s*$", "no"), |
174 | (r"\[(y|yes)/(n|no)\][:?]?\s*$", "y"), |
175 | ): |
176 | if re.search(question, tail, re.I): |
177 | self.chan.send(answer + "\r") |
178 | buf += "\n" |
179 |
|
180 | # One command: its output without the echoed command and the prompt; progress lines keep their last state. |
181 | def cli(self, cmd): |
182 | try: |
183 | self.chan.send(cmd + "\r") |
184 | out = self.read() |
185 | except (OSError, EOFError, paramiko.SSHException) as e: |
186 | raise self.lost(e) |
187 | self.mode = out.rstrip().splitlines()[-1] |
188 | lines = [l.rstrip("\r").split("\r")[-1] for l in out.split("\n")] |
189 | return "\n".join(lines[1:-1]).strip("\n") |
190 |
|
191 | def run_commands(self, cmds, encoding="json", **_): |
192 | try: |
193 | if not self.chan: |
194 | self.open() |
195 | if "(config" in self.mode: |
196 | self.cli("end") |
197 | results = [] |
198 | for i, c in enumerate(cmds): |
199 | # eAPI's { cmd, revision } form is the CLI's "| json revision N". |
200 | cmd, rev = (c["cmd"], c.get("revision")) if isinstance(c, dict) else (c, None) |
201 | show = encoding == "json" and (rev or re.match(r"\s*sh(ow)?\s", cmd)) and not re.search(r"\|\s*json", cmd) |
202 | # pyeapi sends "<cmd> MULTILINE: <text>" (banners) as cmd + input; the CLI reads the text up to EOF. |
203 | head, multi, text = cmd.partition("MULTILINE:") |
204 | # The shell would run what follows a line break as another command; eAPI refuses it too. |
205 | if re.search(r"[\r\n]", head): |
206 | raise ValueError(f"a command may not contain a line break: {head!r}") |
207 | line = "\r".join([head.strip(), *text.strip().splitlines(), "EOF"]) if multi else cmd |
208 | out = self.cli(f"{cmd} | json{f' revision {rev}' if rev else ''}" if show else line) |
209 | errors = [l[2:] for l in out.splitlines() if l.startswith("% ")] |
210 | code, kind = 1002, "invalid command" |
211 | if show and not errors: |
212 | code, kind = 1000, "could not run command" |
213 | try: |
214 | result = json.loads(out) |
215 | except ValueError: |
216 | errors = [out[:200]] |
217 | else: |
218 | errors = result.get("errors") or [] |
219 | if errors: |
220 | raise pyeapi.eapilib.CommandError( |
221 | code, |
222 | f"CLI command {i + 2} of {len(cmds) + 1} '{cmd}' failed: {kind}", |
223 | command_error="; ".join(errors), |
224 | commands=cmds, |
225 | output=results, |
226 | ) |
227 | if encoding != "json": |
228 | result = {"output": out + "\n" if out else ""} |
229 | elif not show: |
230 | # The CLI prints eAPI's warnings as "! " notices. |
231 | notices = [l[2:].strip() for l in out.splitlines() if l.startswith("! ")] |
232 | out = "\n".join(l for l in out.splitlines() if not l.startswith("! ")).strip() |
233 | result = {"messages": [out]} if out and not multi else {} |
234 | if notices: |
235 | result["warnings"] = notices |
236 | results.append(result) |
237 | return results |
238 | except pyeapi.eapilib.ConnectionError: |
239 | if self.chan: |
240 | self.chan.close() |
241 | self.sock = self.chan = None |
242 | raise |
243 |
|
244 |
|
245 | # paramiko logs a traceback when a rebooting switch drops the session. |
246 | logging.getLogger("paramiko").addHandler(logging.NullHandler()) |
247 |
|
248 |
|
249 | # host, or host:port ([v6]:port), else port, else 22. |
250 | def ssh_addr(auth: arista_eos): |
251 | h = auth["host"] |
252 | if "://" in h: |
253 | raise ValueError(f"with transport ssh, host is an address or a hostname (with :port), not a URL: {h}") |
254 | url = urlsplit(f"ssh://{h}" if h.count(":") <= 1 or h.startswith("[") else f"ssh://[{h}]") |
255 | return url.hostname, url.port or int(auth.get("port") or 22) |
256 |
|
257 |
|
258 | # Every action raises DeviceUnreachable when the switch can't be reached and AuthFailure when it refuses the |
259 | # credential, so a flow can retry on result.error.name. Command errors stay pyeapi's CommandError. |
260 | def node(auth: arista_eos, timeout: float = 60): |
261 | eos = open_node(auth, timeout) |
262 | run = eos.run_commands |
263 |
|
264 | def run_commands(*args, **kwargs): |
265 | try: |
266 | return run(*args, **kwargs) |
267 | except pyeapi.eapilib.ConnectionError as e: |
268 | raise connection_error(auth, eos, e) |
269 |
|
270 | eos.run_commands = run_commands |
271 | return eos |
272 |
|
273 |
|
274 | # pyeapi 1.0.4 raises ConnectionError for a socket error (kept in socket_error), an answer that isn't JSON (eAPI |
275 | # still starting, 5xx) and a 401, the only one with neither. |
276 | def connection_error(auth, eos, e): |
277 | # A certificate the resource does not trust is a setting to fix (verify_tls, ca_certificate), not worth a retry. |
278 | if isinstance(eos.connection.socket_error, ssl.SSLCertVerificationError): |
279 | return RuntimeError(f"{auth['host']}: {eos.connection.socket_error}") |
280 | if eos.connection.socket_error is None and e.message != "unable to connect to eAPI": |
281 | return AuthFailure(f"{auth['host']}: {'SSH' if auth.get('transport') == 'ssh' else 'eAPI'} refused the credential ({e.message})") |
282 | return DeviceUnreachable(f"{auth['host']}: {e.message}") |
283 |
|
284 |
|
285 | class DeviceUnreachable(Exception): |
286 | pass |
287 |
|
288 |
|
289 | class AuthFailure(Exception): |
290 | pass |
291 |
|
292 |
|
293 | # pyeapi logs a traceback for every socket error. |
294 | logging.getLogger("pyeapi").addHandler(logging.NullHandler()) |
295 |
|
296 |
|
297 | # host is an address or a URL (https://10.0.0.11:8443); a bare IPv6 address is bracketed. Only https, or http when the |
298 | # resource ticks allow_http (Basic auth then crosses the network in cleartext). pyeapi skips certificate checks unless it |
299 | # is given a context, so one is always passed: verifying, or not when the resource unticks verify_tls (EOS ships a |
300 | # self-signed cert). |
301 | def open_node(auth: arista_eos, timeout: float = 60): |
302 | if auth.get("transport") == "ssh": |
303 | return Ssh(auth, timeout) |
304 | host = auth["host"] |
305 | if "://" not in host: |
306 | host = f"https://[{host}]" if host.count(":") > 1 and "[" not in host else f"https://{host}" |
307 | url = urlsplit(host) |
308 | if not url.hostname or (url.scheme != "https" and not (url.scheme == "http" and auth.get("allow_http"))): |
309 | raise ValueError(f"host must be an address or an https:// URL (http:// needs allow_http on the resource): {auth['host']}") |
310 | # ca_certificate: the PEM of a private CA, trusted instead of the system's CAs. |
311 | context = ssl.create_default_context(cadata=(auth.get("ca_certificate") or "").strip() or None) |
312 | if auth.get("verify_tls") is False: |
313 | context.check_hostname = False |
314 | context.verify_mode = ssl.CERT_NONE |
315 | return pyeapi.connect( |
316 | transport=url.scheme, |
317 | host=url.hostname, |
318 | port=url.port or auth.get("port") or None, |
319 | username=auth["username"], |
320 | password=auth["password"], |
321 | context=context, |
322 | timeout=timeout, |
323 | return_node=True, |
324 | ) |
325 |
|