Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Vitale committed Dec 15, 2017
2 parents 79a93d3 + 91a6674 commit 2897f43
Show file tree
Hide file tree
Showing 260 changed files with 21,437 additions and 287,857 deletions.
7 changes: 1 addition & 6 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from flask import Flask, flash, redirect, render_template, request, session, abort
from flask import Flask
import os
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from config import basedir

app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
Expand All @@ -15,7 +14,3 @@
if __name__ == "__main__":
app.secret_key = os.urandom(25)
manager.run()

# Notes:
# To install python-ldap on Mac OSX, run this command from Terminal:
# export CFLAGS="-I$(xcrun --show-sdk-path)/usr/include/sasl"
26 changes: 14 additions & 12 deletions app/device_classes/deviceType.py
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]
156 changes: 94 additions & 62 deletions app/device_classes/device_definitions/base_device.py
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)
2 changes: 1 addition & 1 deletion app/device_classes/device_definitions/cisco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from cisco_nxos import CiscoNXOS
from cisco_asa import CiscoASA

__all__ = ['CiscoIOS', 'CiscoNXOS', 'CiscoASA']
__all__ = ['CiscoIOS', 'CiscoNXOS', 'CiscoASA']
Loading

0 comments on commit 2897f43

Please sign in to comment.