Skip to content

Commit

Permalink
Update HEREDOC methods to support input of multiple HEREDOCs per command
Browse files Browse the repository at this point in the history
SSL certificates are two HEREDOCs
  • Loading branch information
bewing committed Nov 20, 2017
1 parent c552bed commit cf4ec74
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions napalm/eos/eos.py
Expand Up @@ -55,8 +55,9 @@ class EOSDriver(NetworkDriver):
SUPPORTED_OC_MODELS = []

HEREDOC_COMMANDS = [
"banner login",
"banner motd",
("banner login", 1),
("banner motd", 1),
("protocol https certificate", 2)
]

_RE_BGP_INFO = re.compile('BGP neighbor is (?P<neighbor>.*?), remote AS (?P<as>.*?), .*') # noqa
Expand Down Expand Up @@ -139,12 +140,15 @@ def _lock(self):
raise SessionLockedException('Session is already in use')

@staticmethod
def _multiline_convert(config, start="banner login", end="EOF"):
def _multiline_convert(config, start="banner login", end="EOF", depth=1):
"""Converts running-config HEREDOC into MULTILINE hack"""
ret = list(config) # Don't modify list in-place
try:
s = ret.index(start)
e = ret.index(end, s)
e = s
while depth:
e = ret.index(end, e + 1)
depth = depth - 1
except ValueError: # Couldn't find end, abort
return ret
ret[s] = {'cmd': ret[s], 'input': "\n".join(ret[s+1:e])}
Expand Down Expand Up @@ -177,8 +181,8 @@ def _load_config(self, filename=None, config=None, replace=True):
continue
commands.append(line)

for start in [s for s in self.HEREDOC_COMMANDS if s in commands]:
commands = self._multiline_convert(commands, start)
for start, depth in [(s, d) for (s, d) in self.HEREDOC_COMMANDS if s in commands]:
commands = self._multiline_convert(commands, start=start, depth=depth)

try:
self.device.run_commands(commands)
Expand Down

0 comments on commit cf4ec74

Please sign in to comment.