Skip to content

Commit f41f15a

Browse files
committed
Performance improvements in send_command
1 parent 0402d33 commit f41f15a

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

netmiko/base_connection.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,33 @@ def strip_prompt(self, a_string):
10211021
else:
10221022
return a_string
10231023

1024+
def _first_line_handler(self, data, search_pattern):
1025+
"""
1026+
In certain situations the first line will get repainted which causes a false
1027+
match on the terminating pattern.
1028+
1029+
Filter this out.
1030+
1031+
returns a tuple of (data, first_line_processed)
1032+
1033+
Where data is the original data potentially with the first line modified
1034+
and the first_line_processed is a flag indicating that we have handled the
1035+
first line.
1036+
"""
1037+
try:
1038+
# First line is the echo line containing the command. In certain situations
1039+
# it gets repainted and needs filtered
1040+
lines = data.split(self.RETURN)
1041+
first_line = lines[0]
1042+
if BACKSPACE_CHAR in first_line:
1043+
pattern = search_pattern + r'.*$'
1044+
first_line = re.sub(pattern, repl='', string=first_line)
1045+
lines[0] = first_line
1046+
data = self.RETURN.join(lines)
1047+
return (data, True)
1048+
except IndexError:
1049+
return (data, False)
1050+
10241051
def send_command(self, command_string, expect_string=None,
10251052
delay_factor=1, max_loops=500, auto_find_prompt=True,
10261053
strip_prompt=True, strip_command=True, normalize=True,
@@ -1088,27 +1115,31 @@ def send_command(self, command_string, expect_string=None,
10881115

10891116
i = 1
10901117
output = ''
1118+
first_line_processed = False
1119+
10911120
# Keep reading data until search_pattern is found or until max_loops is reached.
10921121
while i <= max_loops:
10931122
new_data = self.read_channel()
10941123
if new_data:
10951124
if self.ansi_escape_codes:
10961125
new_data = self.strip_ansi_escape_codes(new_data)
1097-
output += new_data
1098-
try:
1099-
lines = output.split(self.RETURN)
1100-
first_line = lines[0]
1101-
# First line is the echo line containing the command. In certain situations
1102-
# it gets repainted and needs filtered
1103-
if BACKSPACE_CHAR in first_line:
1104-
pattern = search_pattern + r'.*$'
1105-
first_line = re.sub(pattern, repl='', string=first_line)
1106-
lines[0] = first_line
1107-
output = self.RETURN.join(lines)
1108-
except IndexError:
1109-
pass
1110-
if re.search(search_pattern, output):
1111-
break
1126+
1127+
# Case where we haven't processed the first_line yet
1128+
if not first_line_processed:
1129+
output += new_data
1130+
output, first_line_processed = self._first_line_handler(
1131+
output,
1132+
search_pattern
1133+
)
1134+
# Check if we have already found our pattern
1135+
if re.search(search_pattern, output):
1136+
break
1137+
1138+
else:
1139+
output += new_data
1140+
# Check if pattern is in the incremental data
1141+
if re.search(search_pattern, new_data):
1142+
break
11121143

11131144
time.sleep(delay_factor * loop_delay)
11141145
i += 1

0 commit comments

Comments
 (0)