Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netmiko #26716

Merged
merged 10 commits into from Jun 1, 2023
Merged

Netmiko #26716

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 30 additions & 10 deletions Packs/Netmiko/Integrations/Netmiko/Netmiko.py
Expand Up @@ -14,12 +14,18 @@
''' HELPER FUNCTIONS '''


# Return only specific keys from dictionary
def include_keys(dictionary, keys):
key_set = set(keys) & set(dictionary.keys())
return {key: dictionary[key] for key in key_set}


def return_file(keys):
return_file.readlines = lambda: keys.split("\n") # type: ignore
return return_file


class Client:
class Client: # pragma: no cover
def __init__(self, platform, hostname, username, password, port, keys):
self.platform = platform
self.hostname = hostname
Expand All @@ -39,7 +45,7 @@ def connect(self):
else:
try:
self.net_connect = Netmiko(device_type=self.platform, host=self.hostname, port=self.port,
use_keys=True, username=self.username, password=self.password)
use_keys=False, username=self.username, password=self.password)
except Exception as err:
return_error(err)

Expand Down Expand Up @@ -73,7 +79,7 @@ def cmds(self, require_exit, exit_argument, commands, enable, isConfig):
return output


def test_command(client):
def test_command(client): # pragma: no cover
client.connect()
client.disconnect()
demisto.results('ok')
Expand All @@ -84,7 +90,7 @@ def cmds_command(client, args):

# Parse the commands
cmds = args.get('cmds')
if type(cmds) != list:
if type(cmds) != list: # pragma: no cover
try:
cmds = cmds.split('\n')
except Exception as err:
Expand Down Expand Up @@ -126,25 +132,38 @@ def cmds_command(client, args):
demisto.error(f"Error with raw print output - {err}")

else:
md = tableToMarkdown(f'Command(s) against {client.hostname} ({client.platform}):', output.get('Commands', []))
hdrs = ["Hostname", "DateTimeUTC", "Command", "Output"]
data = []

# Single command
if len(cmds) == 1:
data.append(output["Commands"][0])

# Multiple commands
else:
for item in output["Commands"]:
data.append(include_keys(item, hdrs))

md = tableToMarkdown(f'Command(s) against {client.hostname} ({client.platform}):', data, headers=hdrs)
outputs_key_field = None
outputs_prefix = None
outputs = None
if not disable_context:
outputs_prefix = "Netmiko"
outputs_key_field = 'DateTimeUTC'
outputs = output.get('Commands')
outputs = output

command_results = CommandResults(
outputs_prefix=outputs_prefix,
outputs_key_field=outputs_key_field,
outputs=outputs,
readable_output=md
)
return_results(command_results)

return command_results


def main():
def main(): # pragma: no cover

params = demisto.params()
args = demisto.args()
Expand Down Expand Up @@ -175,8 +194,9 @@ def main():
if command == 'test-module':
test_command(client)
elif command == 'netmiko-cmds':
cmds_command(client, args)
results = cmds_command(client, args)
return_results(results)


if __name__ in ['__main__', 'builtin', 'builtins']:
if __name__ in ['__main__', 'builtin', 'builtins']: # pragma: no cover
main()