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 all commits
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
51 changes: 33 additions & 18 deletions Packs/Netmiko/Integrations/Netmiko/Netmiko.py
Expand Up @@ -4,22 +4,23 @@
''' IMPORTS '''


import io
import paramiko
import sys
from datetime import datetime

import paramiko
from netmiko import Netmiko


''' HELPER FUNCTIONS '''


def return_file(keys):
return_file.readlines = lambda: keys.split("\n") # type: ignore
return return_file
# 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}


class Client:
class Client: # pragma: no cover
def __init__(self, platform, hostname, username, password, port, keys):
self.platform = platform
self.hostname = hostname
Expand All @@ -33,13 +34,13 @@ def connect(self):
if self.keys:
try:
self.net_connect = Netmiko(device_type=self.platform, host=self.hostname, port=self.port,
pkey=self.keys, use_keys=True, username=self.username, passphrase=self.password)
pkey=self.keys, username=self.username)
except Exception as err:
return_error(err)
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 +74,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 +85,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 +127,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 All @@ -164,19 +178,20 @@ def main():
if ssh_key:
if password:
try:
keys = paramiko.RSAKey.from_private_key(return_file(ssh_key), password=password)
keys = paramiko.RSAKey.from_private_key(io.StringIO(ssh_key), password=password)
except Exception as err:
return_error(f"There was an error - {err} - Did you provide the correct password?")
else:
keys = paramiko.RSAKey.from_private_key(return_file(ssh_key))
keys = paramiko.RSAKey.from_private_key(io.StringIO(ssh_key))

client = Client(platform, hostname, username, password, port, keys)

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()