def __parseInterfaces(self, results): 'str -> List(NetworkInterface)' interfacesList = [] if results: self.windowsDescriptionPattern = self.langBund.getString( 'windows_ipconfig_str_description').strip() self.windowsPhysicalAddressPattern = self.langBund.getString( 'windows_ipconfig_str_physical_address').strip() self.windowsIpAddressPattern = self.langBund.getString( 'windows_ipconfig_str_ip_address').strip() self.windowsNetworkMaskPattern = self.langBund.getString( 'windows_ipconfig_str_mask').strip() self.reqDhcpEnabledPattern = self.langBund.getString( 'windows_ipconfig_req_dhcp_enabled').strip() self.dhcpEnabledPhrase = self.langBund.getString( 'windows_ipconfig_dhcp_enabled_true').strip() interfaceDo = modeling.NetworkInterface('', '', [], [], None, 0) for line in results.split('\n'): if line.strip() == '': if interfaceDo.macAddress and interfaceDo.description: interfacesList.append(interfaceDo) interfaceDo = modeling.NetworkInterface( '', '', [], [], None, 0) matcher = re.match(self.windowsDescriptionPattern, line) if matcher: interfaceDo.description = matcher.group(1).strip() continue matcher = re.match(self.windowsPhysicalAddressPattern, line) if matcher: interfaceDo.macAddress = matcher.group(1).strip() continue matcher = re.match(self.windowsIpAddressPattern, line) if matcher: ipAddr = matcher.group(1).strip() if ip_addr.isValidIpAddress(matcher.group(1).strip()): interfaceDo.ips.append(ip_addr.IPAddress(ipAddr)) if isinstance(ip_addr.IPAddress(ipAddr), (ip_addr.IPv6Address)): interfaceDo.masks.append("") continue matcher = re.match(self.windowsNetworkMaskPattern, line) if matcher: interfaceDo.masks.append(matcher.group(1).strip()) continue matcher = re.match(self.reqDhcpEnabledPattern, line) if matcher: resultStr = matcher.group(1).strip() if resultStr and resultStr.lower( ) == self.dhcpEnabledPhrase: interfaceDo.dhcpEnabled = 1 if interfaceDo.macAddress and interfaceDo.description: interfacesList.append(interfaceDo) return interfacesList raise ValueError("Failed getting interfaces")
def getNatIPFromConfigurationFile(): """ Read IP or IP range from configuration file. @return: A list contains IPAddress objects and IPNetwork objects """ NATIPConfigurationFileFolder = os.path.join( CollectorsParameters.BASE_PROBE_MGR_DIR, CollectorsParameters.getDiscoveryConfigFolder()) NATIPConfigurationFile = os.path.join(NATIPConfigurationFileFolder, 'NATIpAddress.xml') if not os.path.exists(NATIPConfigurationFile): logger.info("There is no NAT IP address defined.") return # Read tags from xml file builder = SAXBuilder() configDoc = builder.build(NATIPConfigurationFile) rootElement = configDoc.getRootElement() ipElements = rootElement.getChildren('Ip') ipRangeElements = rootElement.getChildren('IpRange') NAT_IPs = [] # Read IPAddress, add valid one to NAT_IPs list if ipElements: for ipElement in ipElements: ip = ipElement.getText() if ip_addr.isValidIpAddress(ip): ipObj = ip_addr.IPAddress(ip) NAT_IPs.append(ipObj) # Read IP Ranges, create IPNetwork and add to NAT_IPs list if ipRangeElements: for ipRangeElement in ipRangeElements: ip_range_raw = ipRangeElement.getText() ips = ip_range_raw.split('-') ip_start = ips[0] ip_end = ips[1] if ip_addr.isValidIpAddress(ip_start) and ip_addr.isValidIpAddress( ip_end): ip_start = ip_addr.IPAddress(ip_start) ip_end = ip_addr.IPAddress(ip_end) ips = ip_addr.summarize_address_range(ip_start, ip_end) logger.debug(ips) NAT_IPs.extend(ips) else: logger.warn( "IP Range should start and end with valid IP address") return NAT_IPs
def _parse_address_and_port_from_appl_id(appl_id): m = re.match('(.+)\.(\d+)\.(\d+)$', appl_id) if m: address = m.group(1) port = m.group(2) #Note: When the hexadecimal versions of the IP address (or port number) #starts with 0-9, #they are changed to G-P respectively(for DRDA connections). #For example, "0" is mapped to "G", "1" is mapped to "H", and so on. if address[0] > 'F': TRANSLATE_DICT = { 'G': '0', 'H': '1', 'I': '2', 'J': '3', 'K': '4', 'L': '5', 'M': '6', 'N': '7', 'O': '8', 'P': '9' } address = TRANSLATE_DICT.get(address[0], address[0]) + address[1:] if all(c in string.hexdigits for c in address): address = int(address, 16) return ip_addr.IPAddress(address), int(port)
def __getDnsServerIPs(self): ''' @types: -> list[str] @raise Exception: WMI query failed ''' ips = [] clazz = 'Win32_NetworkAdapterConfiguration' queryBuilder = self._wmiProvider.getBuilder(clazz) queryBuilder.addWmiObjectProperties('dnsServerSearchOrder') queryBuilder.addWhereClause('domainDnsRegistrationEnabled <> NULL') agent = self._wmiProvider.getAgent() dnsServersConfigurationList = agent.getWmiData(queryBuilder) for dnsServersConfiguration in dnsServersConfigurationList: dnsIps = dnsServersConfiguration.dnsServerSearchOrder # depending on protocol this field represented as CSV string # or list of values if not isinstance(dnsIps, types.ListType): dnsIps = map(string.strip, str(dnsIps).split(',')) for ip in dnsIps: if ip: try: if ip_addr.isValidIpAddressNotZero(ip): ips.append(ip_addr.IPAddress(ip)) except: logger.warn('Failed to parse to IP value "%s"' % ip) return ips
def parse_bindings(binding_info, host_ips): """ Parse binding info from binding_info['bindingInformation'] using below format and return list protocol_name, ips, port, hostnamr <network interface ip or "*">:<port>:<host header> :param binding_info: Binding string which have such format: :type binding_info: dict[str, str] :return: list[str, list(IPAddress), int, str] :rtype: list[str, list(IPAddress), int, str] """ binding_string = binding_info.get("bindingInformation") protocol = binding_info.get("protocol") if not binding_string or not protocol: raise ValueError("Incorrect binding's information was passed") tokens = binding_string.split(":") if len(tokens) != 3: raise ValueError("Incorrect binding's string was passed") interface = tokens[0].strip() port = int(tokens[1].strip()) hostname = tokens[2].strip() interfaces = [] if interface == "*": interfaces = host_ips or [] elif interface: interfaces.append(str(ip_addr.IPAddress(interface))) endpoints = [] for interface in interfaces: endpoint = netutils.Endpoint(port, netutils.ProtocolType.TCP_PROTOCOL, interface, portType=protocol) endpoints.append(endpoint) return hostname, protocol, endpoints
def _createHostOsh(self): ''' method creates containing host OSH depending on settings in XML and discovered data ''' appComponent = self.getApplicationComponent() if appComponent.isClustered(): # clustered applications should use weak hosts by IP hostIp = self._applicationIp if hostIp and ip_addr.IPAddress(hostIp).get_is_private(): hostIp = self.getConnectionIp() if not hostIp: raise applications.ApplicationSignatureException( "Cannot report application since no valid host IP is found" ) logger.debug(" -- clustered application uses host by IP '%s'" % hostIp) self.hostOsh = modeling.createHostOSH(hostIp) else: # non-clustered applications use host by hostId self.hostOsh = self.getApplicationComponent().getHostOsh() logger.debug(self.hostOsh) if self.hostOsh: logger.debug(" -- application uses host by Host OSH") else: logger.debug(" -- application uses host by CMDB ID") hostId = self.getApplicationComponent().getHostId() if hostId: self.hostOsh = modeling.createOshByCmdbIdString( 'host', hostId)
def setObjectVectorByResStringArray(objectVector, ipsResult, virtualMode, netAddress=None, netMask=None): # create new network for link object if netAddress and netMask: networkOSHForLink = modeling.createNetworkOSH(netAddress, netMask) else: networkOSHForLink = None # run on the string array (Holding all live pinged ips) for ipResult in ipsResult: isVirtual = 0 # pingedIp - the ip we sent the ping to # replyIp - the ip replied to the ping # Break the curr result by ':' <Reply-IP>:<Pinged-IP> token = ipResult.split(':') if (len(token) == 2): # In case where we ping a virtual ip we get the reply from the real ip # If we are pinging a virtual ip if virtualMode: replyIp, pingedIp = token[0], token[1] isVirtual = 1 else: replyIp, pingedIp = token[1], token[1] else: replyIp, pingedIp = ipResult, ipResult # Create Ip OSH and add to vector pingedIpOSH = modeling.createIpOSH(ip_addr.IPAddress(pingedIp), netMask) objectVector.add(pingedIpOSH) if networkOSHForLink: # Create MEMBER link and set end1(discovered network) and end2(host) objectVector.add( modeling.createLinkOSH('member', networkOSHForLink, pingedIpOSH)) if isVirtual: # Create Ip OSH replyIpOSH = modeling.createIpOSH(replyIp, netMask) # Create a depend link and set end1(pingedIp) and end2(replyIp) newDependLink = modeling.createLinkOSH('depend', pingedIpOSH, replyIpOSH) objectVector.add(replyIpOSH) objectVector.add(newDependLink) if networkOSHForLink: replyIpNetAddress = IPv4(replyIp, netMask).getFirstIp().toString() if replyIpNetAddress == netAddress: # Create MEMBER link and set end1(discovered network) and end2(host) objectVector.add( modeling.createLinkOSH('member', networkOSHForLink, replyIpOSH))
def buildVm(self, server): vm = openstack.Vm() vm.referenced_project = server.getTenantId() vm.name = server.getName() vm.status = server.getStatus() vm.host_id = server.getHostId() vm.id = server.getId() image = server.getImage() if image: logger.debug("server.getImage():", image) vm.image = openstack.Image(image.getId()) flavor = server.getFlavor() if flavor: logger.debug("server.getFlavor():", flavor) vm.flavor = openstack.Flavor(flavor.getId()) getExtendedAttributes = server.getExtendedAttributes() vm.hypervisorHostName = getExtendedAttributes.get( ).getHypervisorHostName() addrs = server.getAddresses().values() for addr in addrs: ip = addr.getAddr() if ip and ip_addr.isValidIpAddressNotZero(ip): vm.ips.append(ip_addr.IPAddress(ip)) return vm
def reportAddressRecord(self, record, zoneOsh): r''' Report address `realization` link between `dns_record` and `ip_address` @types: ResourceRecord, ObjectStateHolder \ -> tuple[ObjectStateHolder, ObjectStateHolder, ObjectStateHolderVector] @raise ValueError: Record is not specified @raise ValueError: Record is not of A type (address) @raise ValueError: Zone OSH is not specified @raise ValueError: Canonical name is not IP address @return: tuple of IP OSH, record OSH itself and resulted vector ''' if not record: raise ValueError("Record is not specified") if not record.type in (ResourceRecord.Type.A, ResourceRecord.Type.AAAA): raise ValueError("Record is not of A type (address)") if not zoneOsh: raise ValueError("Zone OSH is not specified") ipAddress = ip_addr.IPAddress(record.cname) ipOsh = modeling.createIpOSH(ipAddress) recordOsh = self.reportRecord(record, zoneOsh) vector = ObjectStateHolderVector() vector.add(modeling.createLinkOSH('realization', recordOsh, ipOsh)) vector.add(ipOsh) vector.add(recordOsh) return (ipOsh, recordOsh, vector)
def getConnectionUrl(self): if self.url: return self.url url = self.__framework.getParameter('UCS URL') if url: self.url = url return url port = self.__getProtocol().getProtocolAttribute('protocol_port') port = port and port.strip() logger.debug("Setting for port:", port) if not port: port = 80 useHttps = self.__getProtocol().getProtocolAttribute('use_https') logger.debug("Setting for useHttps:", useHttps) useHttps = useHttps and useHttps.strip() if useHttps and useHttps == 'true': protocol = 'https' else: protocol = 'http' if ip_addr.IPAddress(self.ip).get_version() == 6: url = '%s://[%s]:%s/nuova' % (protocol, self.ip, port) else: url = '%s://%s:%s/nuova' % (protocol, self.ip, port) logger.debug("Try connect url:", url) url = url and url.strip() self.url = url return url
def createShellObj(shell, client, ip, langBund, language, codePage, arpMac=None, connectedShellCredId=None): 'Shell, str, langBundle, str, str -> osh' # make sure that 'ip' is an ip and not a dns name # the reason is to make application_ip attribute hold an ip and not a dns name, # hence, when the application will be a trigger it will find the probe clientType = shell.getClientType() if clientType == ClientsConsts.NTCMD_PROTOCOL_NAME: clientType = "ntcmd" logger.debug('creating object for obj_name=%s' % clientType) ipObj = ip if ip_addr.isValidIpAddress(ip): ipObj = ip_addr.IPAddress(ip) else: # maybe it's a hostname? hostname = ip try: ips = SocketDnsResolver().resolve_ips(hostname) ipObj = ips[0] except ResolveException: logger.reportWarning('Could not resolve hostname' + hostname) ipObj = ip shellOsh = ObjectStateHolder(clientType) shellOsh.setAttribute('application_ip', str(ipObj)) shellOsh.setAttribute('data_name', clientType) if clientType != "ntcmd": shellOsh.setAttribute('application_port', shell.getPort()) shellOsh.setContainer(modeling.createHostOSH(str(ipObj))) # UDA client has a property of version, it should be reported if clientType == ClientsConsts.DDM_AGENT_PROTOCOL_NAME: shellOsh.setAttribute('version', client.getVersion()) if (language): shellOsh.setAttribute('language', language) if (codePage): shellOsh.setAttribute('codepage', codePage) shellOsh.setAttribute('credentials_id', shell.getCredentialId()) if arpMac: shellOsh.setAttribute(TopologyConstants.ATTR_APPLICATION_ARP_MAC, arpMac) if connectedShellCredId: shellOsh.setAttribute(TopologyConstants.ATTR_CONN_OS_CRED_ID, connectedShellCredId) return shellOsh
def parse_server(r): name, id_, status, hypervisor_type = imap( r.get, ('Name', 'Id', 'Status', 'Hypervisor Type')) in_maintenance_mode = r.get('Maintenance Mode') in_maintenance_mode = ShowServerCmd.parse_bool(in_maintenance_mode) ip_address = ip_addr.IPAddress(r.get('IP Address')) return ShowServerCmd.Server(name, id_, status, hypervisor_type, in_maintenance_mode, ip_address, r.get('Version'))
def get_remote_devices(self): ''' Creates remote devices dictionary on base of CDP info @type : ->{Str:NetworkDevice} ''' devices = {} connections = self.get_lldp_info() for connection in connections: ip_address = None mac_address = None port_name = None try: if int(connection.lldpRemChassisIdSubtype) == 5: ip_address = int(connection.cdpCacheAddress, 16) ip_address = str(ip_addr.IPAddress(ip_address)) elif int(connection.lldpRemChassisIdSubtype) == 4: mac_address = connection.lldpRemChassisId except: logger.debug('Failed to convert %s to int' % connection.lldpRemChassisIdSubtype) try: if int(connection.lldpRemPortIdSubtype) == 4: ip_address = int(connection.lldpRemPortId, 16) ip_address = str(ip_addr.IPAddress(ip_address)) elif int(connection.lldpRemPortIdSubtype) == 3: mac_address = connection.lldpRemPortId elif int(connection.lldpRemPortIdSubtype) == 7 or int( connection.lldpRemPortIdSubtype) == 5: port_name = str(connection.lldpRemPortId).decode('hex') except: logger.debug('Failed to convert %s to int' % connection.lldpRemPortIdSubtype) port_name = port_name or connection.lldpRemPortDesc device = NetworkDevice() device.ip_address = ip_address device.mac_address = mac_address device.name = connection.lldpRemSysName device.description = connection.lldpRemSysDesc device.port = port_name device.address_id = mac_address or ip_address interface_index = connection.index.split('.')[1] if interface_index: devices[interface_index] = device return devices
def createAddressRecord(name, ip): r''' Create record object of type A with corresponding address validation @types: str, str -> ResourceRecord @raise ValueError: IP Address is not valid ''' ip = ip_addr.IPAddress(ip) recordType = (ip.version == 4 and ResourceRecord.Type.A or ResourceRecord.Type.AAAA) return ResourceRecord(name, recordType, ip)
def discoverTopology(sapUtils, domain, hostname, connInfo): r'''@types: SapUtils, str, str, ConnectionInfo -> oshv, list[str]''' warnings = [] vector = ObjectStateHolderVector() system_name = getSapSystemName(sapUtils) if system_name: system = sap.System(system_name) isSolMan, tmsDomain, instances = consume( warnings, _discoverIsSolutionManager(sapUtils), _discoverTmsDomain(sapUtils), (_discoverInstances(sapUtils, system.getName()) or ())) # find an instance with ip we are connected to connected_instance = find_instance_with_ip( instances, ip_addr.IPAddress(connInfo.ip)) if connected_instance: logger.info( "We have connected to instance %s, adding credentials" % str(connected_instance)) # report currently connected instance system_osh = _reportSystem(sapUtils, system, domain, isSolMan, tmsDomain) connected_osh, additional_vector = sap_abap_discoverer.reportInstanceWithSystem( connected_instance, [ip_addr.IPAddress(connInfo.ip)], system, system_osh, application_ip=ip_addr.IPAddress(connInfo.ip), cred_id=connInfo.credsId) # report all instances vector = second(reportInstances(instances, system, system_osh)) vector.addAll(additional_vector) vector.add(connected_osh) vector.add(system_osh) else: warnings.append( 'Failed to find destination ip among configured server ips. ' 'No Topology will be reported.') else: warnings.append('Failed to find configured servers. ' 'No Topology will be reported.') return vector, warnings
def _get_ip_or_none(ip_string): ip_string = sanitizeIps(ip_string) try: local_ipobject = ip_addr.IPAddress(ip_string) except ValueError: logger.warn('Invalid ip found:' + str(local_ipobject)) return None if str(local_ipobject) == '255.255.255.255': return None return local_ipobject
def discover(shell, powerShellOsh, ipAddress, langBund, Framework, uduid=None): 'Shell, osh, str, Properties, Framework,[str = None, str = None, list(str) = None] -> oshVector' resultVector = ObjectStateHolderVector() hostCmdbid = Framework.getDestinationAttribute('host_cmdbid') hostKey = Framework.getDestinationAttribute('host_key') hostMacs = Framework.getTriggerCIDataAsList('mac_addrs') ipAddrObj = ip_addr.IPAddress(ipAddress) wmiProvider = wmiutils.PowerShellWmiProvider(shell) hostDiscoverer = WmiHostDiscoverer(wmiProvider) hostDo = hostDiscoverer.discover() wmiDnsServersDiscoverer = WmiDnsServersDiscoverer(wmiProvider, ipAddrObj) wmiDnsServersDiscoverer.discover() dnsServersIpList = wmiDnsServersDiscoverer.getResults() wmiWinsServersDiscoverer = WmiWinsServersDiscoverer(wmiProvider, ipAddrObj) wmiWinsServersDiscoverer.discover() winsServersIpList = wmiWinsServersDiscoverer.getResults() dhcpWmiServersDiscoverer = WmiDhcpServersDiscoverer(wmiProvider, ipAddrObj) dhcpWmiServersDiscoverer.discover() dhcpServersIpList = dhcpWmiServersDiscoverer.getResults() interfaceDiscoverer = WmiInterfaceDiscoverer(wmiProvider, ipAddrObj) interfaceDiscoverer.discover() logger.debug('Interfaces successfully discovered via wmic.') try: shellIfaceDiscoverer = IpConfigInterfaceDiscoverer( shell, ipAddrObj, Framework, langBund) shellIfaceDiscoverer.discover() ifaces = shellIfaceDiscoverer.getResults() interfaceDiscoverer.interfacesList.extend(ifaces) except: logger.debugException('') hostDo.ipIsVirtual = interfaceDiscoverer.isIpVirtual() interfacesList = interfaceDiscoverer.getResults() topoBuilder = TopologyBuilder(interfacesList, hostDo, ipAddrObj, powerShellOsh, dnsServersIpList, dhcpServersIpList, winsServersIpList, hostCmdbid, hostKey, hostMacs) topoBuilder.build() # access built host OSH to update UD UID attribute if topoBuilder.hostOsh and uduid: _updateHostUniversalDiscoveryUid(topoBuilder.hostOsh, uduid) topoBuilder.addResultsToVector(resultVector) return resultVector
def _discover_host_ips(host, hostName, hostDnsName, ips_set, allowDnsLookup): '@types: Host, str, str, set[IPAddress], bool -> list[ip_addr.IPAddress]' ips = [] if ip_addr.isValidIpAddress(host.ip): ips = [ip_addr.IPAddress(host.ip)] elif allowDnsLookup: ips = _resolve_host_ips(host, hostName, hostDnsName) if ips and ips_set.issuperset(set(ips)): ips = [] return ips
def _discover_switch_ips(switch, ips_set, allowDnsLookup): '@types: Switch, set[IPAddress], bool -> list[IPAddress]' ips = [] if ip_addr.isValidIpAddress(switch.address): ips = [ip_addr.IPAddress(switch.address)] elif allowDnsLookup: ips = _resolve_switch_ips(switch) if ips and ips_set.issuperset(set(ips)): ips = [] return ips
def _resolve_switch_ips(switch): ''' Resolve address to IPs @types: str -> list[IPAddress]''' address = switch.address if ip_addr.isValidIpAddress(address): return (ip_addr.IPAddress(address),) try: hostname = _get_name_only(switch.name) return dns_resolver.SocketDnsResolver().resolve_ips(hostname) except dns_resolver.ResolveException: return []
def parseIfconfigOutput(ifconfigOutput): splitList = re.split(r"([a-zA-Z0-9]+(?::\d+)?):\s+flags=\d+<([\w,-]+)>", ifconfigOutput) if len(splitList) < 2: logger.warn("Ifconfig output is invalid") return [] splitList = splitList[1:] ipInfoList = [] for i in range(0, len(splitList), 3): ipInfo = _IfconfigRecord() ipInfo.fullInterfaceName = splitList[i] ipFlags = splitList[i + 1] ipInfo.flags = re.split(r",", ipFlags) ipData = splitList[i + 2] matcher = re.match(r"([a-z0-9]+):\d+$", ipInfo.fullInterfaceName) if matcher: ipInfo.physicalInterfaceName = matcher.group(1) else: ipInfo.physicalInterfaceName = ipInfo.fullInterfaceName #ignore loopback if re.match(r"lo\d+$", ipInfo.physicalInterfaceName): continue matcher = re.search( r"\s+inet\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?:-->\s+[\d.]+\s+)?netmask\s+([0-9a-f]+)", ipData) if matcher: ipInfo.ip = matcher.group(1) ipInfo.mask = matcher.group(2) try: matcher = re.search('\s+inet6\s+([\da-fA-F:]+)/(\d+)', ipData) if matcher: ipInfo.ip = ip_addr.IPAddress(matcher.group(1)) ipInfo.mask = matcher.group(2) except: logger.warn('IP Address %s appeared to be invalid' % matcher.group(1)) matcher = re.search(r"\s+zone[\s[^\n]]*([\w.-]+)", ipData) if matcher: ipInfo.zoneName = matcher.group(1) matcher = re.search(r"\s+groupname[\s[^\n]]*([\w.-]+)", ipData) if matcher: ipInfo.ipmpGroupName = matcher.group(1) if ipInfo.ip: ipInfoList.append(ipInfo) else: logger.warn("Could not find assigned IPv4 address for interface '%s'" % ipInfo.physicalInterfaceName) return ipInfoList
def buildDnsServers(self): if self.dnsServerIpList: # accept IPv4 addresses only # currently IPv6 addresses can not be assigned to any probe # and are not a part of reconciliation, so node CI can not be # created based on instance of IPv6 address isIPv4 = lambda ip: ip_addr.IPAddress(ip).get_version() == 4 for dnsIpAddr in filter(isIPv4, self.dnsServerIpList): if ip_addr.isValidIpAddress(dnsIpAddr, filter_client_ip=True): dnsHostOsh = modeling.createHostOSH(str(dnsIpAddr)) dnsAppOsh = modeling.createDnsOsh(str(dnsIpAddr), dnsHostOsh) self.resultVector.add(dnsHostOsh) self.resultVector.add(dnsAppOsh)
def doPortScan(self, ip, tcpPorts, udpPorts): if not ip_addr.isValidIpAddress(ip): raise ValueError("IP address is not valid") tcpRanges = self.splitPortsByRanges(tcpPorts) udpRanges = self.splitPortsByRanges(udpPorts) options = dict.fromkeys(['-v', '-sS', '-n', '-oX', '-p', '"%s"' % str(ip)]) options['-oX'] = '-' if udpRanges: options['-sU'] = None ip_obj = ip_addr.IPAddress(str(ip)) if ip_obj.get_version() == 6: logger.debug("Given ip address have IPv6 format. Using nmap with IPv6 support...") options['-6'] = None options['-p'] = self.genNmapRange(tcpRanges, udpRanges) if options['-p']: cmdLine = self.buildCommandLine(options) else: raise ValueError('Nothing to scan') result = [] if len(cmdLine) > Nmap.LIMIT_CMDLINE: logger.debug("Command line is to big. Splitting...") portsChunk = self.genNmapRangeWithLimit(tcpRanges, udpRanges, Nmap.PORTRANGE_LIMIT) logger.debug("Nmap will be executed %s times" % len(portsChunk)) for portRange in portsChunk: options['-p'] = portRange isTCP = portRange.find('T:') != -1 isUDP = portRange.find('U:') != -1 if isTCP: options['-sS'] = None elif options.has_key('-sS'): del options['-sS'] if isUDP: options['-sU'] = None elif options.has_key('-sU'): del options['-sU'] cmdLine = self.buildCommandLine(options) result.extend(self._execute(cmdLine, self._parseOpenPortsFromXml)) else: result.extend(self._execute(cmdLine, self._parseOpenPortsFromXml)) return result
def _parseOutput(self, ipconfigBuffer, langBund): ips = [] dhcpServerIpPattern = langBund.getString( 'windows_ipconfig_dhcp_server').strip() if ipconfigBuffer: for line in ipconfigBuffer.split('\n'): ipAddrBuffer = re.match(dhcpServerIpPattern, line) if ipAddrBuffer: try: raw_ip = ipAddrBuffer.group(1).strip() if ip_addr.isValidIpAddressNotZero(raw_ip): ips.append(ip_addr.IPAddress(raw_ip)) except: logger.debug('Failed to transform to IP value: %s' % ipAddrBuffer.group(1).strip()) return ips
def parse_from_address(address, resolve_ips_fn): r'@types: str, (str->[ipaddr.IPAddress]?) -> host_parser.HostDescriptor' if not address: raise ValueError('Invalid address') if not resolve_ips_fn: raise ValueError('Invalid resolve_ips_fn') host = None if ip_addr.isValidIpAddress(address): ips = [ip_addr.IPAddress(address)] host = HostDescriptor(ips=ips) else: ips = resolve_ips_fn(address) if is_fqdn(address): host = parse_from_fqdn(address, ips) else: host = HostDescriptor(ips=ips, name=address) return host
def _parseChassisData(self, output): result = [] for chunk in re.split('[\r\n]{4}', output): if not (chunk and chunk.strip()): continue elems = parse_result_as_dict(chunk) chassis = ibm_fsm.Chassis() host_name = elems.get('Hostname', '') if not netutils.isValidIp(host_name): if host_name.find('.') == -1: chassis.hostname = host_name else: chassis.hostname = host_name[:host_name.find('.')] ips_raw = [] ips_raw.extend([ x.strip() for x in elems.get('ipv4', '').strip(' []').split(',') if x and x.strip() ]) ips_raw.extend([ x.strip() for x in elems.get('ipv6', '').strip(' []').split(',') if x and x.strip() ]) for ip_str in ips_raw: if ip_str and ip_addr.isValidIpAddressNotZero(ip_str): chassis.ipList.append(ip_addr.IPAddress(ip_str)) if not (chassis.hostname or chassis.ipList): continue if elems.get('NetInterface'): chassis.iface.append(elems.get('NetInterface')) chassis.name = elems.get('Name') chassis.id = elems.get('Id') chassis.guid = elems.get('GUID') chassis.board_uuid = elems.get('System Board UUID') chassis.serial = elems.get('Serial Number') chassis.frim_level = elems.get('Firmware-level') chassis.firm_build = elems.get('Firmware-build') chassis.firm_name = elems.get('Firmware-name') result.append(chassis) return result
def getConnectionUrl(self): if self.url: return self.url port = self.__getProtocol().getProtocolAttribute('protocol_port') port = port and port.strip() logger.debug("Setting for port:", port) if not port: port = 80 protocol = self.__getProtocol().getProtocolAttribute('protocol') logger.debug("Setting for useHttps:", protocol) if ip_addr.IPAddress(self.ip).get_version() == 6: url = '%s://[%s]:%s' % (protocol, self.ip, port) else: url = '%s://%s:%s' % (protocol, self.ip, port) logger.debug("Try connect url:", url) url = url and url.strip() self.url = url return url
def getHostAddressesbyNetinterface(self, nodeName): ip = None # This command will fail if Network name is not "Public" buff = self.__exec('netint /node:%s /net:Public /prop:Address' % nodeName) endOfHeader = 0 for line in buff.strip().splitlines(): if (line.find('-----') != -1) and (endOfHeader == 0): endOfHeader = 1 continue if endOfHeader == 1: attrs = line.split() if (len(attrs) < 4): continue ip = attrs[4].strip() if ip_addr.isValidIpAddress(ip): ip = ip_addr.IPAddress(ip) break ip = None return (ip and (ip, ) or ())
def _parseOutput(self, ipconfigBuffer, langBund): winsPrimServerIpPattern = self.langBund.getString( 'windows_ipconfig_primary_wins_server').strip() winsSecServerIpPattern = self.langBund.getString( 'windows_ipconfig_secondary_wins_server').strip() ips = [] if ipconfigBuffer: for line in ipconfigBuffer.split('\n'): ipAddrBuffer = (re.match(winsPrimServerIpPattern, line) or re.match(winsSecServerIpPattern, line)) if ipAddrBuffer: try: raw_ip = ipAddrBuffer.group(1).strip() if ip_addr.isValidIpAddressNotZero(raw_ip): ips.append(ip_addr.IPAddress(raw_ip)) except: logger.debug( 'Failed to transform to IP object value %s' % ipAddrBuffer.group(1).strip()) return ips
def __resolve_ips_by_nslookup_without_filter(self, hostname): ipAddressList = [] buffer = self.__get_nslookup_result(hostname) if buffer is not None: matchPat = "Name:\s+.+(?:\.[\w\.-]+)?\s*Address(?:es)?:\s*([\d\.:a-z\,\s]+)" matched = re.search(matchPat, buffer, re.I) if matched: rawAddr = matched.group(1).strip() addrs = re.split('[,\s]+', rawAddr) for addr in addrs: addr = addr.strip() if addr: try: ip_obj = ip_addr.IPAddress(addr) valid = (not ip_obj.is_unspecified and not ip_obj.is_loopback) if valid: ipAddressList.append(ip_obj) except ValueError: pass return ipAddressList