<commit_before># Library for the Class, methods related to Network devices;
#
# Authors: <NAME>
# Created : 2015/10/08
# Modified : 2016/

from lib import netconparser
import ciscointerface


class CiscoNetworkDevice(object):
    """ Class container for all attributes and methods related to a Network Device
    """
    def __init__(self, device_name, user_name, user_password, enable_password, device_type='cisco_ios'):
        self.DeviceName = device_name
        self.UserName = user_name
        self.UPassword = <PASSWORD>
        self.EnablePassword = enable_password
        self.ShowVersion = ''
        self.ShowVersionBrief = ''
        self.ChassisModel = ''
        self.ShowRunning = ''
        self.SystemUpTime = ''
        self.Show_File_System = ''
        self.Interfaces = {}
        self.Vlans = {}
        self.ShowModule = []
        self.ShowCdpNeiDet = []
        self.ShowMacAddress = []
        self.ShowInterfacesStatus = []
        self.ShowInterfaces = []
        self.ShowInterfaceSwitchport = []
        self.ShowInterfaceCapabilities = []
        self.ShowEtherchannelPort = []
        self.VRF = {}
        self.ShowVlan = ''
        self.ListIntLonNam = []
        self.MacAddress = {}

        """ testing using Netmiko as seems stable
        """

        from netmiko import ConnectHandler
        self.Cisco_Device = {
            'device_type': device_type,
            'ip': self.DeviceName,
            'username': self.UserName,
            'password': self.<PASSWORD>,
            'secret': self.EnablePassword,
            }
        self.Device_Connection = ConnectHandler(**self.Cisco_Device)

    def send_command(self, command):
        output = self.Device_Connection.send_command(command)
        return output

    def disconnect(self):
        self.Device_Connection.disconnect()

    def show_version(self):
        self.ShowVersion = self.send_command("sh ver")
        self.ShowVersion = self.ShowVersion.splitlines()
        self.SystemUpTime = netconparser.line_from_text("uptime is", self.ShowVersion)
        self.ShowVersionBrief = netconparser.show_ver_brief(self.ShowVersion)
        self.ChassisModel = netconparser.show_ver_model(self.ShowVersionBrief)

    def show_file_system(self):
        self.Show_File_System = self.send_command("show file systems")
        self.Show_File_System = self.Show_File_System.splitlines()

    def show_module(self):
        self.ShowModule = self.send_command("sh module")
        self.ShowModule = self.ShowModule.splitlines()

    def show_running(self):
        self.ShowRunning = self.send_command("sh run")

    def show_cdp_nei_det(self):
        self.ShowCdpNeiDet = self.send_command("show cdp nei det")
        self.ShowCdpNeiDet = self.ShowCdpNeiDet.splitlines()

    def show_mac_address(self):
        self.ShowMacAddress = self.send_command("show mac address")
        self.ShowMacAddress = self.ShowMacAddress.splitlines()

    def show_int(self):
        self.ShowInterfaces = self.send_command("show interfaces")
        self.ShowInterfaces = self.ShowInterfaces.splitlines()

    def show_int_status(self):
        self.ShowInterfacesStatus = self.send_command("sh int status")
        self.ShowInterfacesStatus = self.ShowInterfacesStatus.splitlines()

    def show_int_switchport(self):
        self.ShowInterfaceSwitchport = self.send_command("sh int switchport")
        self.ShowInterfaceSwitchport = self.ShowInterfaceSwitchport.splitlines()

    def show_int_capabilities(self):
        self.ShowInterfaceCapabilities = self.send_command("sh int capabilities ")
        self.ShowInterfaceCapabilities = self.ShowInterfaceCapabilities.splitlines()

    def show_vlan(self):
        self.ShowVlan = self.send_command("sh vlan")
        self.ShowVlan = self.ShowVlan.splitlines()

    def show_etherchannelport(self):
        self.ShowEtherchannelPort = self.send_command("sh etherchannel port")
        self.ShowEtherchannelPort = self.ShowEtherchannelPort.splitlines()

    def populate_vlans(self):
        """
        :return: {vlan_id_int}: [Vlannumber_str, Vlanname, composite]
        """
        self.show_vlan()
        self.Vlans = netconparser.show_vlan_to_dictionary(self.ShowVlan)

    def populate_interfaces(self):
        """
        runs 'sh int status', 'sh int', 'sh int switchport', 'sh int capabilities';
        and fills in NetworkDevice.Interfaces, dictionary;
        items are Interface classes
        :return:
        """
        self.show_int_status()

        self.show_int()
        listshowint = netconparser.show_interface_to_list(self.ShowInterfaces)

        self.show_int_switchport()
        listshowintswi = netconparser.show_interface_switchport_to_list(self.ShowInterfaceSwitchport)

        for shintperint in listshowint:
            swi_int = ciscointerface.CiscoInterface()
            swi_int.InterfaceName = shintperint[0].split()[0]
            swi_int.InterfaceShortName = netconparser.int_name_to_int_short_name(swi_int.InterfaceName)
            swi_int.ShowInterfacePerInt = shintperint
            self.Interfaces[swi_int.InterfaceShortName] = swi_int
            self.ListIntLonNam.append(swi_int.InterfaceName)

        for shintswiperint in listshowintswi:
            intshortname = shintswiperint[0].split(":")[1].strip()
            self.Interfaces[intshortname].ShowInterfaceSwitchportPerInt = shintswiperint

        self.show_int_capabilities()
        dicshowintcap = netconparser.cut_include_from_list(self.ShowInterfaceCapabilities, self.ListIntLonNam)

        for intkey in self.Interfaces.keys():
            intholder = self.Interfaces[intkey]
            if intholder.InterfaceName in dicshowintcap.keys():
                self.Interfaces[intkey].ShowInterfaceCapabilitiesPerInt = dicshowintcap[intholder.InterfaceName]

            intholder.load_interface_details()

    def populate_mac_address(self):
        self.show_mac_address()
        self.MacAddress = netconparser.show_mac_to_dictionary(self.ShowMacAddress)

    def reset_interfaces(self, list_interfaces):
        self.send_command("conf t")
        for item in list_interfaces:
            self.send_command("int "+item)
            self.send_command("shutdown")
            self.send_command("no shutdown")
        self.send_command("exit")
        return

    def show_int_steroids(self):

        # Working with the IOS version, getting it and presenting a brief.<commit_msg>interface configuration added to class<commit_after># Library for the Class, methods related to Network devices;
#
# Authors: <NAME>
# Created : 2015/10/08
# Modified : 2016/

from lib import netconparser
import ciscointerface
import time


class CiscoNetworkDevice(object):
    """ Class container for all attributes and methods related to a Network Device
    """
    def __init__(self, device_name, user_name, user_password, enable_password, device_type='cisco_ios'):
        self.DeviceName = device_name
        self.UserName = user_name
        self.UPassword = <PASSWORD>
        self.EnablePassword = <PASSWORD>
        self.ShowVersion = ''
        self.ShowVersionBrief = ''
        self.ChassisModel = ''
        self.ShowRunning = ''
        self.SystemUpTime = ''
        self.Show_File_System = ''
        self.Interfaces = {}
        self.Vlans = {}
        self.ShowModule = []
        self.ShowCdpNeiDet = []
        self.ShowMacAddress = []
        self.ShowInterfacesStatus = []
        self.ShowInterfaces = []
        self.ShowInterfaceSwitchport = []
        self.ShowInterfaceCapabilities = []
        self.ShowEtherchannelPort = []
        self.VRF = {}
        self.ShowVlan = ''
        self.ListIntLonNam = []
        self.MacAddress = {}
        self.list_commands = []a =

        """ testing using Netmiko as seems stable
        """

        from netmiko import ConnectHandler
        self.Cisco_Device = {
            'device_type': device_type,
            'ip': self.DeviceName,
            'username': self.UserName,
            'password': self.UPassword,
            'secret': self.EnablePassword,
            }
        self.Device_Connection = ConnectHandler(**self.Cisco_Device)

    def send_command(self, command):
        time.sleep(0.1)
        output = self.Device_Connection.send_command(command)
        return output

    def disconnect(self):
        self.Device_Connection.disconnect()

    def show_version(self):
        self.ShowVersion = self.send_command("sh ver")
        self.ShowVersion = self.ShowVersion.splitlines()
        self.SystemUpTime = netconparser.line_from_text("uptime is", self.ShowVersion)
        self.ShowVersionBrief = netconparser.show_ver_brief(self.ShowVersion)
        self.ChassisModel = netconparser.show_ver_model(self.ShowVersionBrief)

    def show_file_system(self):
        self.Show_File_System = self.send_command("show file systems")
        self.Show_File_System = self.Show_File_System.splitlines()

    def show_module(self):
        self.ShowModule = self.send_command("sh module")
        self.ShowModule = self.ShowModule.splitlines()

    def show_running(self):
        self.ShowRunning = self.send_command("sh run")

    def show_cdp_nei_det(self):
        self.ShowCdpNeiDet = self.send_command("show cdp nei det")
        self.ShowCdpNeiDet = self.ShowCdpNeiDet.splitlines()

    def show_mac_address(self):
        self.ShowMacAddress = self.send_command("show mac address")
        self.ShowMacAddress = self.ShowMacAddress.splitlines()

    def show_int(self):
        self.ShowInterfaces = self.send_command("show interfaces")
        self.ShowInterfaces = self.ShowInterfaces.splitlines()

    def show_int_status(self):
        self.ShowInterfacesStatus = self.send_command("sh int status")
        self.ShowInterfacesStatus = self.ShowInterfacesStatus.splitlines()

    def show_int_switchport(self):
        self.ShowInterfaceSwitchport = self.send_command("sh int switchport")
        self.ShowInterfaceSwitchport = self.ShowInterfaceSwitchport.splitlines()

    def show_int_capabilities(self):
        self.ShowInterfaceCapabilities = self.send_command("sh int capabilities ")
        self.ShowInterfaceCapabilities = self.ShowInterfaceCapabilities.splitlines()

    def show_vlan(self):
        self.ShowVlan = self.send_command("sh vlan")
        self.ShowVlan = self.ShowVlan.splitlines()

    def show_etherchannelport(self):
        self.ShowEtherchannelPort = self.send_command("sh etherchannel port")
        self.ShowEtherchannelPort = self.ShowEtherchannelPort.splitlines()

    def populate_vlans(self):
        """
        :return: {vlan_id_int}: [Vlannumber_str, Vlanname, composite]
        """
        self.show_vlan()
        self.Vlans = netconparser.show_vlan_to_dictionary(self.ShowVlan)

    def populate_interfaces(self):
        """
        runs 'sh int status', 'sh int', 'sh int switchport', 'sh int capabilities';
        and fills in NetworkDevice.Interfaces, dictionary;
        items are Interface classes
        :return:
        """
        self.show_int_status()

        self.show_int()
        listshowint = netconparser.show_interface_to_list(self.ShowInterfaces)

        self.show_int_switchport()
        listshowintswi = netconparser.show_interface_switchport_to_list(self.ShowInterfaceSwitchport)

        for shintperint in listshowint:
            swi_int = ciscointerface.CiscoInterface()
            swi_int.InterfaceName = shintperint[0].split()[0]
            swi_int.InterfaceShortName = netconparser.int_name_to_int_short_name(swi_int.InterfaceName)
            swi_int.ShowInterfacePerInt = shintperint
            self.Interfaces[swi_int.InterfaceShortName] = swi_int
            self.ListIntLonNam.append(swi_int.InterfaceName)

        for shintswiperint in listshowintswi:
            intshortname = shintswiperint[0].split(":")[1].strip()
            self.Interfaces[intshortname].ShowInterfaceSwitchportPerInt = shintswiperint

        self.show_int_capabilities()
        dicshowintcap = netconparser.cut_include_from_list(self.ShowInterfaceCapabilities, self.ListIntLonNam)

        for intkey in self.Interfaces.keys():
            intholder = self.Interfaces[intkey]
            if intholder.InterfaceName in dicshowintcap.keys():
                self.Interfaces[intkey].ShowInterfaceCapabilitiesPerInt = dicshowintcap[intholder.InterfaceName]

            intholder.load_interface_details()

    def populate_mac_address(self):
        self.show_mac_address()
        self.MacAddress = netconparser.show_mac_to_dictionary(self.ShowMacAddress)

    def configure_interfaces(self, list_interfaces, list_commands):
        self.send_command("conf t")
        for interface in list_interfaces:
            self.send_command("int "+interface)
            for command in list_commands:
                self.send_command(command)
        self.send_command("exit")

    def reset_interfaces(self, list_interfaces):
        self.list_commands = ["shutdown", "no shutdown"]
        self.configure_interfaces(list_interfaces,self.list_commands)
        return

    def disable_interfaces(self, list_interfaces):
        self.list_commands = ["shutdown"]
        self.configure_interfaces(list_interfaces,self.list_commands)
        return

    def show_int_steroids(self):

        # Working with the IOS version, getting it and presenting a brief.
{'fraction_non_alphanumeric': 0.09302151263023761, 'fraction_numerical': 0.0025485345926092497, 'mean_word_length': 2.935693215339233, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4514125', 'n_tokens_mistral': 3945, 'n_tokens_neox': 3834, 'n_words': 660}