Skip to content

Commit 1d4d261

Browse files
committed
Improve behavior of normalize_linefeeds() method (ktbyers#3193)
1 parent 134a637 commit 1d4d261

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

netmiko/base_connection.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def __init__(
183183
sock: Optional[socket.socket] = None,
184184
auto_connect: bool = True,
185185
delay_factor_compat: bool = False,
186+
disable_lf_normalization: bool = False,
186187
) -> None:
187188
"""
188189
Initialize attributes for establishing connection to target device.
@@ -294,6 +295,9 @@ def __init__(
294295
:param delay_factor_compat: Set send_command and send_command_timing back to using Netmiko
295296
3.x behavior for delay_factor/global_delay_factor/max_loops. This argument will be
296297
eliminated in Netmiko 5.x (default: False).
298+
299+
:param disable_lf_normalization: Disable Netmiko's linefeed normalization behavior
300+
(default: False)
297301
"""
298302

299303
self.remote_conn: Union[
@@ -315,6 +319,8 @@ def __init__(
315319

316320
# Line Separator in response lines
317321
self.RESPONSE_RETURN = "\n" if response_return is None else response_return
322+
self.disable_lf_normalization = True if disable_lf_normalization else False
323+
318324
if ip:
319325
self.host = ip.strip()
320326
elif host:
@@ -594,7 +600,20 @@ def is_alive(self) -> bool:
594600
def read_channel(self) -> str:
595601
"""Generic handler that will read all the data from given channel."""
596602
new_data = self.channel.read_channel()
597-
new_data = self.normalize_linefeeds(new_data)
603+
604+
if self.disable_lf_normalization is False:
605+
start = time.time()
606+
# Data blocks shouldn't end in '\r' (can cause problems with normalize_linefeeds)
607+
# Only do the extra read if '\n' exists in the output
608+
# this avoids devices that only use \r.
609+
while ("\n" in new_data) and (time.time() - start < 1.0):
610+
if new_data[-1] == "\r":
611+
time.sleep(0.01)
612+
new_data += self.channel.read_channel()
613+
else:
614+
break
615+
new_data = self.normalize_linefeeds(new_data)
616+
598617
if self.ansi_escape_codes:
599618
new_data = self.strip_ansi_escape_codes(new_data)
600619
log.debug(f"read_channel: {new_data}")
@@ -1046,7 +1065,6 @@ def _sanitize_output(
10461065
) -> str:
10471066
"""Strip out command echo and trailing router prompt."""
10481067
if strip_command and command_string:
1049-
command_string = self.normalize_linefeeds(command_string)
10501068
output = self.strip_command(command_string, output)
10511069
if strip_prompt:
10521070
output = self.strip_prompt(output)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy