Beispiel #1
0
	def run(self, args):

		switch = args[0]

		# Get frontend ip for tftp address
		try:
			(frontend, *args) = [host for host in self.owner.call('list.host.interface', ['localhost']) 
				if host['network'] == switch['network']]	
		except:
			raise CommandError(self, '"%s" and the frontend do not share a network' % switch['host'])	

		frontend_tftp_address = frontend['ip']
		switch_address = switch['ip']
		switch_name = switch['host']
		switch_username = self.owner.getHostAttr(switch_name, 'switch_username')
		switch_password = self.owner.getHostAttr(switch_name, 'switch_password')

		# Connect to the switch
		with SwitchDellX1052(switch_address, switch_name, switch_username, switch_password) as _switch:
			_switch.set_tftp_ip(frontend_tftp_address)
			try:
				_switch.connect()
				_switch.upload()
				if self.owner.persistent:
					_switch.apply_configuration()
			except SwitchException as switch_error:
				raise CommandError(self, switch_error)
			except Exception as found_error:
				raise CommandError(self, "There was an error syncing the switch")
Beispiel #2
0
    def run(self, args):

        switch = args[0]

        # Get frontend ip for tftp address
        try:
            (_frontend, *args) = [
                host for host in self.owner.call('list.host.interface',
                                                 ['localhost'])
                if host['network'] == switch['network']
            ]
        except:
            raise CommandError(
                self, '"%s" and the frontend do not share a network' %
                switch['host'])

        frontend_tftp_address = _frontend['ip']
        switch_address = switch['ip']
        switch_name = switch['host']
        switch_username = self.owner.getHostAttr(switch_name,
                                                 'switch_username')
        switch_password = self.owner.getHostAttr(switch_name,
                                                 'switch_password')

        # Check if the switch has an ip address
        if not switch_address:
            raise CommandError(
                self, '"%s" has no address to connect to.' % switch_name)

        # Connect to the switch
        with SwitchDellX1052(switch_address, switch_name, switch_username,
                             switch_password) as switch:
            try:
                switch.set_tftp_ip(frontend_tftp_address)
                switch.connect()
                switch.get_interface_status_table()

                ports = switch.parse_interface_status_table()
                _hosts = self.owner.getHostsForSwitch(switch_name)
                for _port, _, _, _speed, _, _, _state, _, _ in ports:
                    # if there is a host we are managing on the port, show host information
                    if _port in _hosts:
                        host, interface, port, vlan, mac = _hosts[
                            _port].values()
                        self.owner.addOutput(switch_name, [
                            _port, _speed, _state, mac, vlan, host, interface
                        ])
                    else:
                        self.owner.addOutput(
                            switch_name,
                            [_port, _speed, _state, '', '', '', ''])
            except SwitchException as switch_error:
                raise CommandError(self, switch_error)
            except:
                raise CommandError(
                    self,
                    "There was an error getting the status of the switch.")
Beispiel #3
0
	def list_firmware(self, host, switch_attrs):
		kwargs = {
			"username" : switch_attrs.get("switch_username"),
			"password" : switch_attrs.get("switch_password"),
		}

		kwargs = {key: value for key, value in kwargs.items() if value is not None}

		# the context manager ensures we disconnect from the switch
		with SwitchDellX1052(switch_ip_address = host, switchname = host, **kwargs) as x1052_switch:
			x1052_switch.connect()
			return x1052_switch.get_versions()
Beispiel #4
0
	def run(self, args):

		switch = args[0]

		# Get frontend ip for tftp address
		try:
			(_frontend, *args) = [host for host in self.owner.call('list.host.interface', ['localhost'])
					if host['network'] == switch['network']]
		except:
			raise CommandError(self, '"%s" and the frontend do not share a network' % switch['host'])

		# Send traffic through the switch first before requesting mac table
		if self.owner.pinghosts:
			_host_interfaces = [host for host in self.owner.call('list.host.interface')
					if host['network'] == switch['network']]
			for host in _host_interfaces:
				x = subprocess.Popen(['ping', '-c', '1', host['ip']], stdout=subprocess.PIPE)

		frontend_tftp_address = _frontend['ip']
		switch_address = switch['ip']
		switch_name = switch['host']
		switch_username = self.owner.getHostAttr(switch_name, 'switch_username')
		switch_password = self.owner.getHostAttr(switch_name, 'switch_password')

		# Connect to the switch
		with SwitchDellX1052(switch_address, switch_name, switch_username, switch_password) as switch:
			try:
				switch.set_tftp_ip(frontend_tftp_address)
				switch.connect()
				switch.get_mac_address_table()

				hosts = switch.parse_mac_address_table()
				for _vlan, _mac, _port, _ in hosts:
					row = self.owner.db.select("""
						nodes.name, networks.device FROM nodes, networks
						WHERE networks.node=nodes.id AND networks.mac=%s
					""", (_mac,))

					if row:
						_hostname, _interface = row[0]
						self.owner.addOutput(switch_name,
							[_port, _mac, _hostname, _interface, _vlan])

			except SwitchException as switch_error:
				raise CommandError(self, switch_error)
			except:
				raise CommandError(self, "There was an error getting the mac address table")
Beispiel #5
0
	def getStatus(self, switch, make, model):
		s = None
		status = None
		u = stack.api.get.GetHostAttr(switch, 'switch_username')
		p = stack.api.get.GetHostAttr(switch, 'switch_password')

		if (make, model) == ('Mellanox', 'm7800'):
			s = SwitchMellanoxM7800(switch, username = u, password = p)
		elif (make, model) == ('DELL', 'x1052'):
			s = SwitchDellX1052(switch, username = u, password = p)

		if s:
			try:
				s.connect()
				status = 'up'
			except:
				pass

		return status
Beispiel #6
0
    def update_firmware(self,
                        switch_name,
                        tftp_ip,
                        software_file_path=None,
                        boot_file_path=None,
                        **kwargs):
        """Updates the software and/or boot firmware on the provided switch."""
        # No need to continue if we weren't provided any software to upgrade
        if software_file_path is None and boot_file_path is None:
            return

        try:
            x1052_switch = SwitchDellX1052(switch_ip_address=switch_name,
                                           switchname=switch_name,
                                           **kwargs)
            x1052_switch.set_tftp_ip(ip=tftp_ip)
            x1052_switch.connect()
            # Upload the new software to the switch
            if software_file_path is not None:
                x1052_switch.upload_software(software_file=software_file_path)

            # Upload the new boot firmware to the switch
            if boot_file_path is not None:
                x1052_switch.upload_boot(boot_file=boot_file_path)

            # Reboot the switch to apply the updates
            x1052_switch.reload()
        # Turn some potentially verbose and detailed error messages into something more end user friendly
        # while keeping the dirty details available in the logs.
        except (SwitchException, ExpectMoreException) as exception:
            stack.commands.Log(
                message=
                f"Error during firmware update on {switch_name}: {exception}",
                level=syslog.LOG_ERR)
            raise CommandError(
                cmd=self.owner,
                msg=f"Failed to update firmware on {switch_name}.")
Beispiel #7
0
    def run(self, args):

        switch = args[0]

        # Get frontend ip for tftp address
        try:
            (_frontend, *args) = [
                host for host in self.owner.call('list.host.interface',
                                                 ['localhost'])
                if host['network'] == switch['network']
            ]
        except:
            raise CommandError(
                self, '"%s" and the frontend do not share a network' %
                switch['host'])

        frontend_tftp_address = _frontend['ip']
        switch_address = switch['ip']
        switch_name = switch['host']
        switch_username = self.owner.getHostAttr(switch_name,
                                                 'switch_username')
        switch_password = self.owner.getHostAttr(switch_name,
                                                 'switch_password')

        list_switch_host = self.owner.call('list.switch.host', [switch_name])

        # Connect to the switch
        with SwitchDellX1052(switch_address, switch_name, switch_username,
                             switch_password) as switch:
            try:
                switch.set_tftp_ip(frontend_tftp_address)
                switch.connect()
                switch.download()

                filename = '%s/%s' % (switch.tftpdir, switch.current_config)
                with open(filename, 'r') as f:
                    lines = f.readlines()
                    _printline = True
                    port = None
                    porttype = None
                    vlan = 'untagged'
                    for line in lines:
                        if not self.owner.raw:
                            if 'crypto' in line:
                                break

                            if 'gigabitethernet' in line:
                                port = line.split('/')[-1].strip()

                            l = line.split()
                            if len(l) == 3 and l[0] == 'switchport' and l[
                                    1] == 'mode':
                                porttype = l[2].strip()

                            if len(l) == 4 and l[0] == 'switchport' \
                              and l[1] == porttype:
                                vlan = l[3].strip()

                            if '!' in line and port and porttype:
                                host = None
                                interface = None
                                for o in list_switch_host:
                                    oport = '%s' % o['port']
                                    if oport == port:
                                        host = o['host']
                                        interface = o['interface']
                                        break

                                self.owner.addOutput(
                                    switch_name,
                                    [port, vlan, porttype, host, interface])

                                port = None
                                porttype = None
                                vlan = 'untagged'

                        # This is the line that gets hit if raw=true
                        else:
                            if _printline:
                                print(line, end='')
            except SwitchException as switch_error:
                raise CommandError(self, switch_error)
            except Exception:
                raise CommandError(
                    self,
                    "There was an error downloading the running config from the switch"
                )
Beispiel #8
0
#!/opt/stack/bin/python3.6

from stack.switch.x1052 import SwitchDellX1052
from stack.api import Call

#switch_ip = '39.87.48.58'
switch_ip = '192.168.2.1'

switch = SwitchDellX1052(switch_ip, 'admin', 'admin')


def test_tftp_ip():
    switch.set_tftp_ip('1.1.1.1')
    assert switch.stacki_server_ip == '1.1.1.1'
Beispiel #9
0
    def run(self, args):

        switch = args[0]

        # Get frontend ip for tftp address
        try:
            (_frontend, *args) = [
                host for host in self.owner.call('list.host.interface',
                                                 ['localhost'])
                if host['network'] == switch['network']
            ]
        except:
            raise CommandError(
                self, '"%s" and the frontend do not share a network' %
                switch['host'])

        frontend_tftp_address = _frontend['ip']
        switch_address = switch['ip']
        switch_name = switch['host']
        switch_username = self.owner.getHostAttr(switch_name,
                                                 'switch_username')
        switch_password = self.owner.getHostAttr(switch_name,
                                                 'switch_password')

        # Connect to the switch
        with SwitchDellX1052(switch_address, switch_name, switch_username,
                             switch_password) as switch:
            try:
                switch.set_tftp_ip(frontend_tftp_address)
                switch.connect()
                switch.download()

                filename = '%s/%s' % (switch.tftpdir, switch.current_config)
                with open(filename, 'r') as f:
                    lines = f.readlines()
                    _printline = True
                    _block = {}
                    for line in lines:
                        if not self.owner.raw:
                            if 'crypto' in line:
                                break

                            if 'gigabitethernet' in line:
                                _block['port'] = line.split('/')[-1].strip()

                            if 'switchport' in line and 'access' in line:
                                _, _type, _, _vlan = line.split()
                                _block['type'] = _type
                                _block['vlan'] = _vlan

                            if '!' in line and _block:
                                try:
                                    self.owner.addOutput(
                                        switch_name, [
                                            _block['port'],
                                            _block['vlan'],
                                            _block['type'],
                                        ])
                                except:
                                    pass
                                _block = {}

                        # This is the line that gets hit if raw=true
                        else:
                            if _printline:
                                print(line, end='')
            except SwitchException as switch_error:
                raise CommandError(self, switch_error)
            except Exception:
                raise CommandError(
                    self,
                    "There was an error downloading the running config from the switch"
                )