Skip to content

Commit

Permalink
Very rough first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Mar 7, 2020
1 parent 5360368 commit 7f91bbb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
36 changes: 30 additions & 6 deletions netmiko/base_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from netmiko.ssh_exception import (
NetmikoTimeoutException,
NetmikoAuthenticationException,
ConfigInvalidException,
)
from netmiko.utilities import (
write_bytes,
Expand Down Expand Up @@ -1664,7 +1665,7 @@ def send_config_set(
config_mode_command=None,
cmd_verify=True,
enter_config_mode=True,
config_error_str="",
error_pattern="",
):
"""
Send configuration commands down the SSH channel.
Expand Down Expand Up @@ -1701,8 +1702,9 @@ def send_config_set(
:param enter_config_mode: Do you enter config mode before sending config commands
:type exit_config_mode: bool
:param config_error_str: Error string to look for in device output
:type config_error_str: str
:param error_pattern: Regular expression pattern to detect config errors in the
output.
:type error_pattern: str
"""
delay_factor = self.select_delay_factor(delay_factor)
if config_commands is None:
Expand All @@ -1726,14 +1728,31 @@ def send_config_set(
output += self._read_channel_timing(
delay_factor=delay_factor, max_loops=max_loops
)
if error_pattern:
if re.search(error_pattern, output, flags=re.M):
msg = f"""
Invalid input detected in output. Netmiko and fast_cli do not allow detection of
the specific command that failed. Please disable fast_cli to find the specific command
that failed."""
raise ConfigInvalidException(msg)
elif not cmd_verify:
output = ""
for cmd in config_commands:
self.write_channel(self.normalize_cmd(cmd))
time.sleep(delay_factor * 0.05)
if error_pattern:
output += self._read_channel_timing(
delay_factor=delay_factor, max_loops=max_loops
)
if re.search(error_pattern, output, flags=re.M):
msg = f"Invalid input detected at command: {cmd}"
raise ConfigInvalidException(msg)

# Gather output
output += self._read_channel_timing(
delay_factor=delay_factor, max_loops=max_loops
)
if not error_pattern:
output += self._read_channel_timing(
delay_factor=delay_factor, max_loops=max_loops
)
else:
for cmd in config_commands:
self.write_channel(self.normalize_cmd(cmd))
Expand All @@ -1751,6 +1770,11 @@ def send_config_set(
new_output = self.read_until_pattern(pattern=pattern)
output += new_output

if error_pattern:
if re.search(error_pattern, output, flags=re.M):
msg = f"Invalid input detected at command: {cmd}"
raise ConfigInvalidException(msg)

if exit_config_mode:
output += self.exit_config_mode()
output = self._sanitize_output(output)
Expand Down
6 changes: 6 additions & 0 deletions netmiko/ssh_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ class NetmikoAuthenticationException(AuthenticationException):
pass


class ConfigInvalidException(Exception):
"""Exception raised for invalid configuration error."""

pass


NetMikoTimeoutException = NetmikoTimeoutException
NetMikoAuthenticationException = NetmikoAuthenticationException

0 comments on commit 7f91bbb

Please sign in to comment.