-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
260 changed files
with
21,437 additions
and
287,857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
from device_definitions.cisco import CiscoIOS, CiscoNXOS, CiscoASA | ||
|
||
# The keys of this dictionary are the supported device_types | ||
CLASS_MAPPER= { | ||
'cisco_ios': CiscoIOS, | ||
'cisco_iosxe': CiscoIOS, | ||
'cisco_nxos': CiscoNXOS, | ||
'cisco_asa': CiscoASA | ||
CLASS_MAPPER = { | ||
'cisco_ios': CiscoIOS, | ||
'cisco_iosxe': CiscoIOS, | ||
'cisco_nxos': CiscoNXOS, | ||
'cisco_asa': CiscoASA | ||
} | ||
|
||
platforms = list(CLASS_MAPPER.keys()) | ||
platforms.sort() | ||
platforms_str = "\n".join(platforms) | ||
platforms_str = "\n" + platforms_str | ||
|
||
|
||
def DeviceHandler(*args, **kwargs): | ||
"""Factory function selects the proper class and creates object based on ios_type.""" | ||
if kwargs['ios_type'] not in platforms: | ||
raise ValueError('Unsupported ios_type: currently supported platforms are: {}'.format(platforms_str)) | ||
DeviceClass = device_dispatcher(kwargs['ios_type']) | ||
return DeviceClass(*args, **kwargs) | ||
"""Select the proper class and creates object based on ios_type.""" | ||
if kwargs['ios_type'] not in platforms: | ||
raise ValueError('Unsupported ios_type: currently supported platforms are: {}'.format(platforms_str)) | ||
DeviceClass = device_dispatcher(kwargs['ios_type']) | ||
return DeviceClass(*args, **kwargs) | ||
|
||
|
||
def device_dispatcher(ios_type): | ||
"""Select the class to be instantiated based on vendor/platform.""" | ||
return CLASS_MAPPER[ios_type] | ||
"""Select the class to be instantiated based on vendor/platform.""" | ||
return CLASS_MAPPER[ios_type] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,99 @@ | ||
from ...scripts_bank.lib import netmiko_functions as nfn | ||
from ...scripts_bank.lib import functions as fn | ||
from flask import g, session | ||
|
||
|
||
class BaseDevice(object): | ||
"""Base device object for all device vendors and models.""" | ||
|
||
def __init__(self, id, hostname, ipv4_addr, type, ios_type): | ||
"""Initialization function.""" | ||
self.id = id | ||
self.hostname = hostname | ||
self.ipv4_addr = ipv4_addr | ||
self.type = type | ||
self.ios_type = ios_type | ||
|
||
def __del__(self): | ||
"""Deletion function.""" | ||
pass | ||
|
||
def enter_config_mode(self, activeSession): | ||
"""Enter configuration mode on device using existing SSH session.""" | ||
nfn.runEnterConfigModeInSession(activeSession) | ||
|
||
def exit_config_mode(self, activeSession): | ||
"""Exit configuration mode on device using existing SSH session.""" | ||
nfn.runExitConfigModeInSession(activeSession) | ||
|
||
def run_ssh_command(self, command, activeSession): | ||
"""Execute single command on device using existing SSH session.""" | ||
return nfn.runSSHCommandInSession(command, activeSession) | ||
|
||
def run_ssh_config_commands(self, cmdList, activeSession): | ||
"""Execute configuration commands on device. | ||
Execute one or more configuration commands on device. | ||
Commands provided via array, with each command on it's own array row. | ||
Uses existing SSH session. | ||
""" | ||
return nfn.runMultipleSSHConfigCommandsInSession(cmdList, activeSession) | ||
|
||
def run_multiple_commands(self, command, activeSession): | ||
"""Execute multiple commands on device using existing SSH session.""" | ||
newCmd = [] | ||
for x in self.split_on_newline(command): | ||
newCmd.append(x) | ||
nfn.runMultipleSSHCommandsInSession(newCmd, activeSession) | ||
|
||
def run_multiple_config_commands(self, command, activeSession): | ||
"""Execute multiple configuration commands on device. | ||
Execute multiple configuration commands on device. | ||
Commands provided via array, with each command on it's own array row. | ||
Saves configuration settings to memory on device once completed. | ||
Uses existing SSH session. | ||
""" | ||
newCmd = [] | ||
for x in self.split_on_newline(command): | ||
newCmd.append(x) | ||
# Get command output from network device | ||
result = nfn.runMultipleSSHConfigCommandsInSession(newCmd, activeSession) | ||
saveResult = self.save_config_on_device(activeSession) | ||
for x in saveResult: | ||
result.append(x) | ||
return result | ||
|
||
def split_on_newline(self, x): | ||
"""Split string into an array by each newline in string.""" | ||
return fn.splitOnNewline(x) | ||
|
||
def get_cmd_output(self, command, activeSession): | ||
"""Get SSH command output and returns it as an array. | ||
Executes command on device using existing SSH session. | ||
Stores and returns output in an array. | ||
Each array row is separated by newline. | ||
""" | ||
result = self.run_ssh_command(command, | ||
activeSession) | ||
return self.split_on_newline(result) | ||
|
||
def get_cmd_output_with_commas(self, command, activeSession): | ||
"""Execute command on device and replaces spaces with commas. | ||
Executes command on device using existing SSH session. | ||
Stores and returns output in an array. | ||
Replaces all spaces in returned output with commas. | ||
Each array row is separated by newline. | ||
""" | ||
result = self.run_ssh_command(command, activeSession) | ||
result = self.replace_double_spaces_commas(result) | ||
return self.split_on_newline(result) | ||
|
||
def replace_double_spaces_commas(self, x): | ||
"""Replace all double spaces in provided string with a single comma.""" | ||
return fn.replaceDoubleSpacesCommas(x) | ||
|
||
def __init__(self, id, hostname, ipv4_addr, type, ios_type): | ||
self.id = id | ||
self.hostname = hostname | ||
self.ipv4_addr = ipv4_addr | ||
self.type = type | ||
self.ios_type = ios_type | ||
|
||
def __del__(self): | ||
pass | ||
|
||
def return_stored_ssh(self): | ||
return sshStore | ||
|
||
def enter_config_mode(self, activeSession): | ||
nfn.runEnterConfigModeInSession(activeSession) | ||
|
||
def exit_config_mode(self, activeSession): | ||
nfn.runExitConfigModeInSession(activeSession) | ||
|
||
def run_ssh_command(self, command, activeSession): | ||
return nfn.runSSHCommandInSession(command, activeSession) | ||
|
||
def run_ssh_config_commands(self, cmdList, activeSession): | ||
return nfn.runMultipleSSHConfigCommandsInSession(cmdList, activeSession) | ||
|
||
def run_multiple_commands(self, command, activeSession): | ||
newCmd = [] | ||
for x in self.split_on_newline(command): | ||
newCmd.append(x) | ||
result = nfn.runMultipleSSHCommandsInSession(newCmd, activeSession) | ||
|
||
def run_multiple_config_commands(self, command, activeSession): | ||
newCmd = [] | ||
for x in self.split_on_newline(command): | ||
newCmd.append(x) | ||
# Get command output from network device | ||
result = nfn.runMultipleSSHConfigCommandsInSession(newCmd, activeSession) | ||
saveResult = self.save_config_on_device(activeSession) | ||
for x in saveResult: | ||
result.append(x) | ||
return result | ||
|
||
# Splits string into an array by each newline ('\n') in string | ||
def split_on_newline(self, x): | ||
return fn.splitOnNewline(x) | ||
|
||
# Gets SSH command output and returns it as an array, with each line separated by a newline ('\n') | ||
def get_cmd_output(self, command, activeSession): | ||
result = self.run_ssh_command(command, activeSession) | ||
return self.split_on_newline(result) | ||
|
||
def get_cmd_output_with_commas(self, command, activeSession): | ||
result = self.run_ssh_command(command, activeSession) | ||
result = self.replace_double_spaces_commas(result) | ||
return self.split_on_newline(result) | ||
|
||
def replace_double_spaces_commas(self, x): | ||
return fn.replaceDoubleSpacesCommas(x) | ||
|
||
def find_prompt_in_session(self, activeSession): | ||
return nfn.findPromptInSession(activeSession) | ||
def find_prompt_in_session(self, activeSession): | ||
"""Return device prompt from existing SSH session.""" | ||
return nfn.findPromptInSession(activeSession) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.