netmiko session: one open/close/is_alive/_send_command for the SSH CLI drivers #8

Open
opened 2026-10-06 17:31:29 +00:00 by christianmanivong · 1 comment
Owner

Five drivers carry the same netmiko session code.

open()

  • napalm_netgear/netgear_smart.py:130 and napalm_tplink_jetstream/tplink_jetstream.py:103 are identical.
  • napalm_openwrt/openwrt.py:98 is the same without enable().
  • napalm_linux/linux.py:285 is the same plus stty and package-manager detection.

is_alive()

  • Identical in netgear_smart.py:159, openwrt.py:127 and tplink_jetstream.py:131.
  • The same check on self._device instead of self.device in linux.py:322 and napalm_procurve/procurve.py:221; procurve adds a REST branch.

_send_command() has the same shape in netgear_smart.py:172, tplink_jetstream.py:149 and procurve.py:353: a single command or a list, then the fallback, with socket errors mapped to ConnectionClosedException.

  • The inner _do_send closure is identical in all three.
  • What differs is the error markers: "Invalid"/"Error", "% Invalid"/"Error", and "Invalid input"/"% Invalid"/"Error".
  • The expect string is rebuilt inline each time, although it equals each driver's own _exec_prompt().

Exit status. _split_status, which splits a command's output from its echo <marker>$?, exists in napalm_linux/linux.py:98 and napalm_openwrt/system_mixin.py:29.

  • linux strips terminal codes and removes only the last marker.
  • openwrt removes every marker and keeps terminal codes.

One implementation belongs next to _send_command.

Also: netgear_smart.py:148 raises a German message ("Verbindungsaufbau fehlgeschlagen …"), and the message text belongs in English.

Proposal

  • A NetmikoSessionMixin in napalm_device_types/netmiko_session.py with concrete open, close, is_alive and _send_command.
  • Hooks: _CLI_ERROR_MARKERS, _command_prompt() (None for a shell without expect string), _ENABLE_ON_OPEN, _after_open().
  • One name for the session attribute.
  • napalm>=4 already brings netmiko, so this adds no dependency.

This is the base NAPALM/napalm-device-types#9 builds on.

Prio P3: five copies of the transport layer; a fix in one (for example a timeout) does not reach the others.

From the NetOrk function index (NetOrk/netork#670); every location was checked against the current code. Paths are relative to each driver repo.

Five drivers carry the same netmiko session code. **`open()`** - `napalm_netgear/netgear_smart.py`:130 and `napalm_tplink_jetstream/tplink_jetstream.py`:103 are identical. - `napalm_openwrt/openwrt.py`:98 is the same without `enable()`. - `napalm_linux/linux.py`:285 is the same plus `stty` and package-manager detection. **`is_alive()`** - Identical in `netgear_smart.py`:159, `openwrt.py`:127 and `tplink_jetstream.py`:131. - The same check on `self._device` instead of `self.device` in `linux.py`:322 and `napalm_procurve/procurve.py`:221; procurve adds a REST branch. **`_send_command()`** has the same shape in `netgear_smart.py`:172, `tplink_jetstream.py`:149 and `procurve.py`:353: a single command or a list, then the fallback, with socket errors mapped to `ConnectionClosedException`. - The inner `_do_send` closure is identical in all three. - What differs is the error markers: "Invalid"/"Error", "% Invalid"/"Error", and "Invalid input"/"% Invalid"/"Error". - The expect string is rebuilt inline each time, although it equals each driver's own `_exec_prompt()`. **Exit status.** `_split_status`, which splits a command's output from its `echo <marker>$?`, exists in `napalm_linux/linux.py`:98 and `napalm_openwrt/system_mixin.py`:29. - linux strips terminal codes and removes only the last marker. - openwrt removes every marker and keeps terminal codes. One implementation belongs next to `_send_command`. **Also:** `netgear_smart.py`:148 raises a German message ("Verbindungsaufbau fehlgeschlagen …"), and the message text belongs in English. **Proposal** - A `NetmikoSessionMixin` in `napalm_device_types/netmiko_session.py` with concrete `open`, `close`, `is_alive` and `_send_command`. - Hooks: `_CLI_ERROR_MARKERS`, `_command_prompt()` (None for a shell without expect string), `_ENABLE_ON_OPEN`, `_after_open()`. - One name for the session attribute. - napalm>=4 already brings netmiko, so this adds no dependency. This is the base NAPALM/napalm-device-types#9 builds on. Prio P3: five copies of the transport layer; a fix in one (for example a timeout) does not reach the others. From the NetOrk function index (NetOrk/netork#670); every location was checked against the current code. Paths are relative to each driver repo.
Author
Owner

Re-homed by docs/STRUCTURE.md (NAPALM/napalm-device-types#37, NetOrk/netork#1105): napalm-device-types stays free of transport code, so a shared netmiko session does not go here. If it is worth sharing, it belongs in a CLI-driver helper package; otherwise it stays in the drivers.

Re-homed by `docs/STRUCTURE.md` (NAPALM/napalm-device-types#37, NetOrk/netork#1105): napalm-device-types stays free of transport code, so a shared netmiko session does not go here. If it is worth sharing, it belongs in a CLI-driver helper package; otherwise it stays in the drivers.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: NAPALM/napalm-device-types#8