diff --git a/napalm/base/base.py b/napalm/base/base.py index 6676a151e..1259f6966 100644 --- a/napalm/base/base.py +++ b/napalm/base/base.py @@ -737,7 +737,9 @@ def get_bgp_config( """ raise NotImplementedError - def cli(self, commands: List[str]) -> Dict[str, Union[str, Dict[str, Any]]]: + def cli( + self, commands: List[str], encoding: str = "text" + ) -> Dict[str, Union[str, Dict[str, Any]]]: """ Will execute a list of commands and return the output in a dictionary format. diff --git a/napalm/base/mock.py b/napalm/base/mock.py index e785528fd..bff491e20 100644 --- a/napalm/base/mock.py +++ b/napalm/base/mock.py @@ -151,7 +151,9 @@ def close(self) -> None: def is_alive(self) -> models.AliveDict: return {"is_alive": self.opened} - def cli(self, commands: List[str]) -> Dict[str, Union[str, Dict[str, Any]]]: + def cli( + self, commands: List[str], encoding: str = "text" + ) -> Dict[str, Union[str, Dict[str, Any]]]: count = self._count_calls("cli") result = {} regexp = re.compile("[^a-zA-Z0-9]+") diff --git a/napalm/eos/eos.py b/napalm/eos/eos.py index 0d01370fa..893d535c0 100644 --- a/napalm/eos/eos.py +++ b/napalm/eos/eos.py @@ -891,7 +891,9 @@ def get_lldp_neighbors_detail(self, interface=""): ) return lldp_neighbors_out - def cli(self, commands): + def cli(self, commands, encoding="text"): + if encoding not in ("text", "json"): + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = {} if type(commands) is not list: @@ -899,9 +901,11 @@ def cli(self, commands): for command in commands: try: - cli_output[str(command)] = self._run_commands( - [command], encoding="text" - )[0].get("output") + result = self._run_commands([command], encoding=encoding) + if encoding == "text": + cli_output[str(command)] = result[0]["output"] + else: + cli_output[str(command)] = result[0] # not quite fair to not exploit rum_commands # but at least can have better control to point to wrong command in case of failure except pyeapi.eapilib.CommandError: diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index bd0edcd2b..9104c31dd 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -2362,7 +2362,7 @@ def get_arp_table(self, vrf=""): arp_table.append(entry) return arp_table - def cli(self, commands): + def cli(self, commands, encoding="text"): """ Execute a list of commands and return the output in a dictionary format using the command as the key. @@ -2375,6 +2375,8 @@ def cli(self, commands): 'show clock': u'*22:01:51.165 UTC Thu Feb 18 2016'} """ + if encoding not in ("text",): + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = dict() if type(commands) is not list: raise TypeError("Please enter a valid list of commands!") diff --git a/napalm/iosxr/iosxr.py b/napalm/iosxr/iosxr.py index 8036151ee..3d3697513 100644 --- a/napalm/iosxr/iosxr.py +++ b/napalm/iosxr/iosxr.py @@ -885,7 +885,9 @@ def get_lldp_neighbors_detail(self, interface=""): return lldp_neighbors - def cli(self, commands): + def cli(self, commands, encoding="text"): + if encoding not in ("text",): + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = {} diff --git a/napalm/iosxr_netconf/iosxr_netconf.py b/napalm/iosxr_netconf/iosxr_netconf.py index 0b8b901b8..656078880 100644 --- a/napalm/iosxr_netconf/iosxr_netconf.py +++ b/napalm/iosxr_netconf/iosxr_netconf.py @@ -1326,7 +1326,7 @@ def get_lldp_neighbors_detail(self, interface=""): return lldp_neighbors_detail - def cli(self, commands): + def cli(self, commands, encoding="text"): """Execute raw CLI commands and returns their output.""" return NotImplementedError diff --git a/napalm/junos/junos.py b/napalm/junos/junos.py index cebf46f7b..db3daf751 100644 --- a/napalm/junos/junos.py +++ b/napalm/junos/junos.py @@ -1012,8 +1012,10 @@ def get_lldp_neighbors_detail(self, interface=""): return lldp_neighbors - def cli(self, commands): + def cli(self, commands, encoding="text"): """Execute raw CLI commands and returns their output.""" + if encoding not in ("text",): + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = {} def _count(txt, none): # Second arg for consistency only. noqa diff --git a/napalm/nxos/nxos.py b/napalm/nxos/nxos.py index 775868b9a..d857832e4 100644 --- a/napalm/nxos/nxos.py +++ b/napalm/nxos/nxos.py @@ -1127,7 +1127,11 @@ def get_bgp_neighbors(self) -> Dict[str, models.BGPStateNeighborsPerVRFDict]: results[vrf_name] = result_vrf_dict return results - def cli(self, commands: List[str]) -> Dict[str, Union[str, Dict[str, Any]]]: + def cli( + self, commands: List[str], encoding: str = "text" + ) -> Dict[str, Union[str, Dict[str, Any]]]: + if encoding not in ("text",): + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output: Dict[str, Union[str, Dict[str, Any]]] = {} if type(commands) is not list: raise TypeError("Please enter a valid list of commands!") diff --git a/napalm/nxos_ssh/nxos_ssh.py b/napalm/nxos_ssh/nxos_ssh.py index 61ee35a08..01147d0e6 100644 --- a/napalm/nxos_ssh/nxos_ssh.py +++ b/napalm/nxos_ssh/nxos_ssh.py @@ -787,7 +787,9 @@ def get_bgp_neighbors(self): # FIX -- need to merge IPv6 and IPv4 AFI for same neighbor return bgp_dict - def cli(self, commands): + def cli(self, commands, encoding="text"): + if encoding not in ("text",): + raise NotImplementedError("%s is not a supported encoding" % encoding) cli_output = {} if type(commands) is not list: raise TypeError("Please enter a valid list of commands!") diff --git a/test/junos/TestJunOSDriver.py b/test/junos/TestJunOSDriver.py index 2f8d5e0ae..c477bc541 100644 --- a/test/junos/TestJunOSDriver.py +++ b/test/junos/TestJunOSDriver.py @@ -86,7 +86,7 @@ def read_txt_file(self, filename): with open(filename) as data_file: return data_file.read() - def cli(self, command=""): + def cli(self, command="", encoding="text"): return self.read_txt_file( "junos/mock_data/{parsed_command}.txt".format( parsed_command=command.replace(" ", "_") diff --git a/test/junos/conftest.py b/test/junos/conftest.py index a0a057328..dc21dce60 100644 --- a/test/junos/conftest.py +++ b/test/junos/conftest.py @@ -119,7 +119,7 @@ def close(self): def bind(*args, **kvargs): pass - def cli(self, command=""): + def cli(self, command="", encoding="text"): filename = "{safe_command}.txt".format(safe_command=self.sanitize_text(command)) fielpath = self.find_file(filename) return self.read_txt_file(fielpath)