def __parseLsMapSeaInfo(self, output): ''' str -> list(_SeaInfo)''' nameToSea = {} for entry in re.split( "------ --------------------------------------------", output): matcher = re.match( "\s*(\w+)\s.*SEA\s+(\w+).*Backing device\s+(\w+)", entry, re.DOTALL) if matcher: # shared Ethernet adapter name name = matcher.group(2).strip() sea = nameToSea.get(name) if not sea: # backing device mapped to virtual server eth adapter bkDevName = matcher.group(3).strip() index = self._getDevNameAndIndex(bkDevName)[1] backingInterface = Interface(name=bkDevName, index=index, mac=index) sea = _SeaInfo(Sea(name), backingInterface) nameToSea[name] = sea #the virtual server Ethernet adapter vIfaceName = matcher.group(1).strip() index = self._getDevNameAndIndex(vIfaceName)[1] virtualInterface = Interface(name=vIfaceName, index=index, mac=index) sea.virtualInterfaces.append(virtualInterface) return nameToSea.values()
def __parseInterfacesInLanscanOutput(self, output): '''str -> map(str, networking.Interface) # Expected format for HP-UX lanscan command -- #0/0/0/1/0 0x00306E4989E7 0 UP lan0 snap0 1 ETHER Yes 119 #0/0/12/0/0 0x00306E4C999B 1 UP lan1 snap1 2 ETHER Yes 119 #0/0/14/0/0 0x00306E4A4773 2 UP lan2 snap2 3 ETHER Yes 119 ''' nameToInterface = {} #The first two lines are skipped because they contain output header for line in output.split('\n'): properties = line.strip().split() if len(properties) > 3: status = properties[3] # get only live interfaces with valid hardware path if status.lower() == 'up': hwPath = properties[0] name = properties[4] index = self.__getDevNameAndIndex(name)[1] # check whether parsing is correct try: if index == int(properties[2]): # strip 0x from the mac macStr = properties[1] mac = self.__parseLanscanMacString(macStr) or index hpuxRole = _HpuxInterfaceRole(hardwarePath=hwPath) nic = Interface(name=name, index=index, mac=mac) if self.__getSystemVersion() in ['10.20']: nic.serviceIndex = properties[6] nic._addRole(hpuxRole) nameToInterface[name] = nic except: logger.warnException('Wrong line format: %s' % line) return nameToInterface
def __parseLstcpipInterfacesInfo(self, output): 'str -> list(_InterfaceInfo)' infos = [] for line in output.split('\n'): matchObj = re.match('(e[nt]+\d+)\s+(.*?)\s+(.*?)\s+(.*?)\s(.*)', line.strip()) if matchObj: status = matchObj.group(4) name = matchObj.group(1) index = self._getDevNameAndIndex(name)[1] interface = Interface(name = name, index = index) info = _InterfaceInfo(interface) if status.lower() == 'up': status = _InterfaceInfo.STATUS_UP mac = matchObj.group(5) interface.mac = mac.strip() netmask = matchObj.group(3) ipAddress = matchObj.group(2) info.ips.append( Ip(ipAddress, netmask) ) else: status = _InterfaceInfo.STATUS_DETACHED interface.mac = index info.status = status infos.append(info) return infos
def __parseInterfacesInLanscanOutput(self, output): '''str -> map(str, networking.Interface) # Expected format for HP-UX lanscan command -- #0/0/0/1/0 0x00306E4989E7 0 UP lan0 snap0 1 ETHER Yes 119 #0/0/12/0/0 0x00306E4C999B 1 UP lan1 snap1 2 ETHER Yes 119 #0/0/14/0/0 0x00306E4A4773 2 UP lan2 snap2 3 ETHER Yes 119 ''' nameToInterface = {} #The first two lines are skipped because they contain output header for line in output.split('\n'): properties = line.strip().split() if len(properties) > 3: status = properties[3] # get only live interfaces with valid hardware path if status.lower() == 'up': hwPath = properties[0] name = properties[4] index = self.__getDevNameAndIndex(name)[1] # check whether parsing is correct try: if index == int(properties[2]): # strip 0x from the mac macStr = properties[1] mac = self.__parseLanscanMacString(macStr) or index hpuxRole = _HpuxInterfaceRole(hardwarePath = hwPath) nic = Interface(name = name, index = index, mac = mac) if self.__getSystemVersion() in ['10.20']: nic.serviceIndex = properties[6] nic._addRole(hpuxRole) nameToInterface[name] = nic except: logger.warnException('Wrong line format: %s' % line) return nameToInterface
def __parseLstcpipInterfacesInfo(self, output): 'str -> list(_InterfaceInfo)' infos = [] for line in output.split('\n'): matchObj = re.match('(e[nt]+\d+)\s+(.*?)\s+(.*?)\s+(.*?)\s(.*)', line.strip()) if matchObj: status = matchObj.group(4) name = matchObj.group(1) index = self._getDevNameAndIndex(name)[1] interface = Interface(name=name, index=index) info = _InterfaceInfo(interface) if status.lower() == 'up': status = _InterfaceInfo.STATUS_UP mac = matchObj.group(5) interface.mac = mac.strip() netmask = matchObj.group(3) ipAddress = matchObj.group(2) info.ips.append(Ip(ipAddress, netmask)) else: status = _InterfaceInfo.STATUS_DETACHED interface.mac = index info.status = status infos.append(info) return infos
def discoverLinkAggregations(self, aixNetworking=None): ''' Discover networking related to link aggregations topology only list(networking.Interfaces) or None -> networking.UnixNetworking''' logger.debug("Discover Link Aggregation interfaces") aixNetworking = aixNetworking or UnixNetworking() nics = self.discoverAvailableInterfaces() nameToNic = {} map(lambda nic, nameToNic=nameToNic: nameToNic.update({nic.name: nic}), nics) # filter only link aggregations linkAggregations = filter( lambda nic: str(nic.description).lower().count("etherchannel"), nics) for interface in linkAggregations: logger.debug("Found LA: %s" % interface) nic = aixNetworking.getInterfaceByName(interface.name) if not nic: nic = interface aixNetworking.addInterface(nic) aggregationRole = AggregationRole() nic._addRole(aggregationRole) try: names = self._getMemberNamesOfLinkAggr(nic) logger.debug( 'Gather aggregation information for names %s of %s' % (names, nic)) for name in names: aggregatedInterface = aixNetworking.getInterfaceByName( name) if not aggregatedInterface: aggregatedInterface = nameToNic.get(name) if not aggregatedInterface: index = self._getDevNameAndIndex(name)[1] aggregatedInterface = Interface(name=name, index=index, mac=index) aixNetworking.addInterface(aggregatedInterface) aggregatedRole = _AggregatedRole() aggregatedRole.setAggregatingInterface(nic) aggregatedInterface._addRole(aggregatedRole) if not netutils.isValidMac( aggregatedInterface.mac) and netutils.isValidMac( nic.mac): aggregatedInterface.mac = nic.mac logger.debug('aggregated %s' % aggregatedInterface) aggregationRole.addInterface(aggregatedInterface) except Exception, e: logger.warn(str(e)) self._gatherIpInformation(nic, aixNetworking)
def __parseInterfaceInfoInIfconfigOutput(self, output): 'str -> list(_InterfaceInfo)' nameToInfo = {} ifname = None for line in output.split('\n'): # first line match interface name m = re.match('([aelostv][ilnopt][0-9]+):\\s+flags=', line) if m: ifname = m.group(1) continue # second line match ip information m = re.match('\s+inet (\d+\.\d+\.\d+\.\d+) netmask 0x([0-9a-f]+)', line) or re.match('\s+inet6 ([\da-fA-F:]+)/(\d+)', line) if m and ifname: ip = m.group(1) netmask = m.group(2) try: ip = Ip(ip, netmask) info = nameToInfo.get(ifname) if not info: index = self._getDevNameAndIndex(ifname)[1] interface = Interface(name=ifname, mac=index, index=index) info = _InterfaceInfo(interface) nameToInfo[ifname] = info info.ips.append(ip) except InvalidIpException, iie: logger.warn('Cannot create ip "%s". %s ' % (ip, str(iie))) except InvalidNetmaskException, ine: logger.warn('Cannot create ip with netmask "%s". %s' % (netmask, str(ine)))
def __discoverClimInterface(self, climName): """ @types: string -> None @raise ValueError: when command "gtacl -cv "climcmd %s /sbin/ifconfig -a % <clim_name>" gives no output or fails """ cmd = "climcmd %s /sbin/ifconfig -a" % climName cmdOutput = self.__shell.execCmd('gtacl -cv "%s"' % cmd) if not cmdOutput or self.__shell.getLastCmdReturnCode() != 0: raise ValueError('Failed to get CLIM') (header, interfaceData) = cmdOutput.split(cmd) if header and interfaceData: interfacesByName = {} matches = ShellDiscoverer.__INTERFACE_REGEXP.findall(interfaceData) for match in matches: name = match[0] uniqueName = "%s.%s" % (climName, match[0]) mac= match[1] if netutils.isValidMac(mac): interface = Interface(netutils.parseMac(mac), uniqueName) parentInterfaceName = self.__getParentInterfaceName(name) if parentInterfaceName and interfacesByName.has_key(parentInterfaceName): parentInterface = interfacesByName[parentInterfaceName] aliasRole = AliasRole() aliasRole.parentInterface = parentInterface interface._addRole(aliasRole) self.__networking.addInterface(interface) interfacesByName[name] = interface matches = ShellDiscoverer.__INTERFACE_AND_IP_REGEXP.findall(interfaceData) for match in matches: name = match[0] ip = match[2] netmask = match[4] if netutils.isValidIp(ip) and netutils.isValidIp(netmask): if interfacesByName.has_key(name): interface = interfacesByName[name] self.__networking.addIpAndNetwork(ip, netmask, interface.name) else: self.__networking.addIpAndNetwork(ip, netmask) else: logger.warn('Unrecognized output') logger.reportWarning("Failed to discover CLIM network interfaces")
def __parseAggregatedInterfacesInLanscanOutput(self, lanscanOutput): ''' Returns mapping of link aggregation index to aggregated interfaces str -> map(int, list(networking.Interface))''' indexToInterfaces = {} for line in _split(lanscanOutput): if re.match(r"[\d\s]+", line): indices = line.split() linkAggregationIndex = indices[0] indices = map(lambda index: int(index), indices[1:]) if indices: for index in indices: nic = Interface(index = index, mac = index) name = self.__getInterfaceName(nic) nic.name = name indexToInterfaces.setdefault(linkAggregationIndex, []).append(nic) return indexToInterfaces
def __parseAggregatedInterfacesInLanscanOutput(self, lanscanOutput): ''' Returns mapping of link aggregation index to aggregated interfaces str -> map(int, list(networking.Interface))''' indexToInterfaces = {} for line in _split(lanscanOutput): if re.match(r"[\d\s]+", line): indices = line.split() linkAggregationIndex = indices[0] indices = map(lambda index: int(index), indices[1:]) if indices: for index in indices: nic = Interface(index=index, mac=index) name = self.__getInterfaceName(nic) nic.name = name indexToInterfaces.setdefault(linkAggregationIndex, []).append(nic) return indexToInterfaces
def _parseInterfaceSpeedViaDladmS11(self, output): if not output: raise ValueError('Failed to parse empty output') result = [] for line in output.split('\n'): items = line.split(':') if len(items) == 2: ifaceName = items[0] ifaceSpeed = long(items[1]) * 1000000 result.append(Interface(name=ifaceName, speed=long(ifaceSpeed))) return result
def __parseInterfacesInIoscanOutput(self, ioscanOutput): 'str -> map(str, networking.Interface)' nameToInterfaces = {} for line in _split(ioscanOutput): lanProperties = line.split(":") if line.count(":lan:") and len(lanProperties) > 16: hardwarePath = lanProperties[10] try: interfaceIndex = int(lanProperties[12]) except: logger.warn('Cannot parse interface index from value: %s' % lanProperties[12]) else: description = lanProperties[17] nic = Interface(description = description, index = interfaceIndex, mac = interfaceIndex) hpuxRole = _HpuxInterfaceRole(hardwarePath = hardwarePath) nic._addRole(hpuxRole) name = self.__getInterfaceName(nic) nic.name = name nameToInterfaces[name] = nic return nameToInterfaces
def discoverLinkAggregations(self, aixNetworking = None): ''' Discover networking related to link aggregations topology only list(networking.Interfaces) or None -> networking.UnixNetworking''' logger.debug("Discover Link Aggregation interfaces") aixNetworking = aixNetworking or UnixNetworking() nics = self.discoverAvailableInterfaces() nameToNic = {} map(lambda nic, nameToNic = nameToNic: nameToNic.update({nic.name : nic}), nics) # filter only link aggregations linkAggregations = filter(lambda nic: str(nic.description).lower().count("etherchannel"), nics) for interface in linkAggregations: logger.debug("Found LA: %s" % interface) nic = aixNetworking.getInterfaceByName(interface.name) if not nic: nic = interface aixNetworking.addInterface(nic) aggregationRole = AggregationRole() nic._addRole(aggregationRole) try: names = self._getMemberNamesOfLinkAggr(nic) logger.debug('Gather aggregation information for names %s of %s' % (names, nic)) for name in names: aggregatedInterface = aixNetworking.getInterfaceByName(name) if not aggregatedInterface: aggregatedInterface = nameToNic.get(name) if not aggregatedInterface: index = self._getDevNameAndIndex(name)[1] aggregatedInterface = Interface(name = name, index = index, mac = index) aixNetworking.addInterface(aggregatedInterface) aggregatedRole = _AggregatedRole() aggregatedRole.setAggregatingInterface(nic) aggregatedInterface._addRole(aggregatedRole) if not netutils.isValidMac(aggregatedInterface.mac) and netutils.isValidMac(nic.mac): aggregatedInterface.mac = nic.mac logger.debug('aggregated %s' % aggregatedInterface) aggregationRole.addInterface(aggregatedInterface) except Exception, e: logger.warn(str(e)) self._gatherIpInformation(nic, aixNetworking)
def _parseInterfaceSpeedViaDladm(self, output): if not output: raise ValueError('Failed to parse empty output') result = [] for line in output.split('\n'): m = re.search('([\w\-]+).*?speed:\s*(\d+)\s*(Mbps|Gbps)', line) if m: ifaceName = m.group(1) ifaceSpeedEnt = m.group(3) ifaceSpeed = ifaceSpeedEnt == 'Mbps' and long(m.group(2)) * 1000000 or long(m.group(2)) * 1000000000 result.append(Interface(name=ifaceName, speed=long(ifaceSpeed))) return result
def __parseInterfacesInIoscanOutput(self, ioscanOutput): 'str -> map(str, networking.Interface)' nameToInterfaces = {} for line in _split(ioscanOutput): lanProperties = line.split(":") if line.count(":lan:") and len(lanProperties) > 16: hardwarePath = lanProperties[10] try: interfaceIndex = int(lanProperties[12]) except: logger.warn('Cannot parse interface index from value: %s' % lanProperties[12]) else: description = lanProperties[17] nic = Interface(description=description, index=interfaceIndex, mac=interfaceIndex) hpuxRole = _HpuxInterfaceRole(hardwarePath=hardwarePath) nic._addRole(hpuxRole) name = self.__getInterfaceName(nic) nic.name = name nameToInterfaces[name] = nic return nameToInterfaces
def __parseIoscliLsdevOutput(self, output): 'str -> list(networking.Interface)' interfaces = [] for line in output.split('\n'): line = line.strip() if not line: continue tokens = line.split(':') name = tokens[0] if name.startswith('ent'): description = len(tokens) > 1 and tokens[2] index = self._getDevNameAndIndex(name)[1] interfaces.append( Interface(mac=index, name=name, description=description, index=index)) return interfaces
def __parseInterfacesInLsDevOutput(self, output): 'str -> list(networking.Interface)' interfaces = [] for line in output.split('\n'): line = line.strip() if not line: continue tokens = line.split(':') name = tokens[0] if name.startswith('ent'): description = len(tokens) > 1 and tokens[1] prefix, index = self._getDevNameAndIndex(name) if prefix and index is not None: interface = Interface(name=name, description=description, index=index, mac=index) interfaces.append(interface) return interfaces
def __parseAggregationInterfacesFromLanscan(self, output): '''str -> map(str, Interface) Output example: LinkAgg0 0x001560043C1A 900 UP lan900 snap900 19 ETHER Yes 119 LinkAgg1 0x000000000000 901 DOWN lan901 snap901 20 ETHER Yes 119 ''' nameToInterface = {} for line in output.split('\n'): properties = line.strip().split() if len(properties) > 3: status = properties[3] if status.lower() == 'up': macStr = properties[1] index = properties[2] name = properties[4] mac = self.__parseLanscanMacString(macStr) or index nic = Interface(name=name, index=index, mac=mac) nameToInterface[name] = nic return nameToInterface
def __discoverRegularInterfaces(self): """ @raise ValueError: when command "gtacl -p scf info lif '$zzlan.*'" gives no output of fails """ interfacesData = self.__shell.execCmd("gtacl -p scf info lif '$zzlan.*'") if not interfacesData or self.__shell.getLastCmdReturnCode() != 0: raise ValueError("Failed to discover regular interfaces") lines = [line.strip() for line in interfacesData.split('\n') if line and re.match(r"\$ZZLAN", line.strip(), re.I)] for line in lines: interfaceData = line.split() # Four means the number of groups in valid output string describing interface if len(interfaceData) != 4: logger.warn("Output format is not supported: %s" % line) continue if interfaceData[3].lower() != 'ethernet': logger.info("Interface type %s was skipped." % interfaceData[3]) continue mac = interfaceData[2] if netutils.isValidMac(mac): mac = netutils.parseMac(mac) else: logger.warn("Interface is skipped -- MAC address is invalid: %s" % mac) continue m = re.match(r"\$ZZLAN\.(.*)", interfaceData[0], re.I) if m: name = m.group(1) else: logger.warn("Interface is skipped -- name was not found in line: %s" % line) continue description = interfaceData[1] interface = Interface(mac, name, description) self.__getNetworking().addInterface(interface)
def __parseInterfacesFromNetconfFile(self, output): 'str -> _DhcpInterfaceStatusInfo' infos = [] lines = output.split('\n') ifaceIndexToNameDict = {} for line in lines: ifaceIndexToNameMatch = re.match( '\s*INTERFACE_NAME\[(\d+)\]\s*=\s*[\"\']*([\w:\.]+)[\"\']*.*', line) if ifaceIndexToNameMatch: index = ifaceIndexToNameMatch.group(1) name = ifaceIndexToNameMatch.group(2).strip() ifaceIndexToNameDict[index] = name for line in lines: isDhcpEnabledMatch = re.match( '\s*DHCP_ENABLE\[(\d+)\]\s*=\s*(\d+).*', line) if isDhcpEnabledMatch: index = isDhcpEnabledMatch.group(1) isDhcpEnabled = isDhcpEnabledMatch.group(2) != '0' name = ifaceIndexToNameDict.get(index) infos.append( _DhcpInterfaceStatusInfo(Interface(name=name, index=index), isDhcpEnabled)) return infos
def __parseInterfaceInEntstatOutput(self, devname, output): 'str, str -> networking.Interface or None' #type m = re.search('Device Type:\s+(.+)', output) type = m and m.group(1).strip() #mac m = re.search('Hardware Address: ([0-9a-f:]{17})', output) rawMac = m and m.group(1) if type and rawMac: mac = None index = self._getDevNameAndIndex(devname)[1] if netutils.isValidMac(rawMac): mac = netutils.parseMac(rawMac) else: logger.warn("Mac value for interface %s is not valid '%s'" % (devname, rawMac)) mac = index m = re.search('Media Speed Running:\s+(\d+)', output) speed = m and long(m.group(1)) * 1000 return Interface(mac=mac, description=type, name=devname, index=index, speed=speed)
def __parseStatesOfInterfacesInNetstatOutput(self, output): '''str -> list(_InterfaceState) @output: lan900:1 1500 10.112.192.0 10.112.192.13 82305206 0 6755 0 0 ''' states = [] for line in output.strip().split('\n')[1:]: properties = line.strip().split() if len(properties) > 3: #we should skip 'header' lines of the output if properties[1].lower() == 'mtu': continue #netstat will report interface name with '*' as suffix in case #interface is configured but disabled, at least this is a default #behavior for configured but disabled interface allias name = properties[0].strip('*') linkAggregationIndex = self.__getDevNameAndIndex(name)[1] # set index by default as aggregation interface index and alias index concatenation # for name lan900:1 we have to get alias index as 9001 tokens = name.split(':') if len(tokens) > 1: ind = re.match('(\d+)', tokens[1]) index = '%s%s' % (linkAggregationIndex, ind and ind.group(1) or '') else: index = linkAggregationIndex ipStr = properties[3] m = re.match('([\da-fA-F\:]+)', properties[2]) if m: try: tmpIp = ip_addr.IPAddress(m.group(1)) if tmpIp and tmpIp.version == 6: ipStr = m.group(1) except: pass try: nic = Interface(name=name, index=index, mac=index) ips = None try: ips = self.getIp(nic) logger.debug(ips) except: logger.warn( 'Failed to get IP information for interface %s' % nic.name) # When interface has several IP addresses getIp() method returns data only for the first one. # If additional data is obtained for current IP address, then this data is used. # If obtained additional data is for different IP address, this data is skipped. # logger.debug('ipStr %s' % ipStr) if not ips: ips = [Ip(ip_addr.IPAddress(ipStr))] states.append(_InterfaceState(nic, ips)) except: logger.warn( 'Failed to parse interface information for line: %s' % line) logger.debugException('') return states
if not nic: vInterface = nameToNic.get(interface.name) vInterface = vInterface or interface vioNetworking.addInterface(vInterface) role = _SeaVirtualInterfaceRole(sea) vInterface._addRole(role) logger.debug('Virtual %s' % vInterface) #added since in Sprint env SEAs do have ips assigned. if interfaceInfoList: for info in interfaceInfoList: seaName, seaIndex = self._getDevNameAndIndex( sea.name()) if (info.status == _InterfaceInfo.STATUS_UP and (sea.name() == info.interface.name or self._isAlias( Interface(name=sea.name(), index=seaIndex), info.interface))): for ip in info.ips: logger.debug('For interface %s discovered %s' % (sea.name(), ip)) vioNetworking.addIpAndNetwork( ip.ip, ip.netmask, sea.name()) return vioNetworking def discoverNetworking(self, vioNetworking=None): '-> networking.UnixNetworking' logger.debug('Discover whole VIO networking information') vioNetworking = vioNetworking or VioNetworking() self.discoverSeaNetworking(vioNetworking) ShellDiscoverer.discoverNetworking(self, vioNetworking) return vioNetworking