def discoverExchangeServers(self): #@return list of ExchangeServer Do hostname = self._getHostNameByHostname() try: dagList = self._getClusteredConfiguration() except: dagList = [] logger.debug('The exchange configuration is not a clustered one.') logger.debugException('') mdbList = [] try: mdbList = self._getMailboxDatabaseConfiguration() ipResolver = dns_resolver.NsLookupDnsResolver(self._shell) for mdb in mdbList: if mdb.servers: for serverInfo in mdb.servers: try: serverInfo.ips = ipResolver.resolve_ips( serverInfo.name) except: logger.debug( 'Failed to resolve ip for server %s ' % serverInfo.name) except: logger.debug('Failed to discover Mailbox Database configuration.') logger.debugException('') exchangeServers = self._getExchangeServers(hostname, hostname) if exchangeServers: for exchangeServer in exchangeServers: exchangeServer.dagList = dagList exchangeServer.mdbList = mdbList return exchangeServers
def resolveFQDN(shell, ip): ''' @deprecated: use dns_resolver.NsLookupDnsResolver and dns_resolver.SocketDnsResolver Resolves fqdn of a host by ip. NsLookup is used first and then @netutils.getHostName used on fallback @types: ip -> str? ''' fqdn = None if isValidIp(ip): dnsResolver = dns_resolver.NsLookupDnsResolver(shell) fqdn = dnsResolver.resolve_hostnames(ip) if not fqdn: try: hostnames = dns_resolver.SocketDnsResolver().resolve_hostnames( ip) if hostnames: fqdn = hostnames[0] except: logger.warn('Failed to resolve host IP through socket') else: fqdn = fqdn[0] return fqdn
def resolveFQDNByNsLookup(self, dnsName): """ @deprecated: Use dns_resolver.NsLookupDnsResolver Resolves (or not) FQDN by given machine name @param dnsName: the machine name to resolve FQDN @type dnsName: string @rtype: string """ resolver = dns_resolver.NsLookupDnsResolver(self.shell) fqdn = resolver.resolve_fqdn(dnsName) return fqdn
def resolveDnsNameByNslookup(self, ipAddr, dnsServer=''): """ @deprecated: Use dns_resolver.NsLookupDnsResolver Resolves (or not) machine DNS name by given IP @param dnsName: the machine name to resolve IPs @type dnsName: string @rtype: string @return: IP address if resolved; None if not resolved """ resolver = dns_resolver.NsLookupDnsResolver(self.shell) dnsNames = resolver.resolve_hostnames(ipAddr) if dnsNames: return dnsNames[0]
def resolveIPByNsLookup(dnsServers, shell, hostname): ipAddresses = [] logger.debug('Try to resolve host name by nslookup:%s' % hostname) if dnsServers: for dns in dnsServers: dnsResolver = dns_resolver.NsLookupDnsResolver(shell, dns_server=dns) inetAddresses = dnsResolver.resolve_ips_without_filter(hostname) logger.debug('find ip address: ', inetAddresses) if inetAddresses: for inetAddress in inetAddresses: if not str(inetAddress) in ipAddresses: ipAddresses.append(str(inetAddress)) else: dnsResolver = dns_resolver.NsLookupDnsResolver(shell) inetAddresses = dnsResolver.resolve_ips_without_filter(hostname) logger.debug('find ip address: ', inetAddresses) if inetAddresses: for inetAddress in inetAddresses: if not str(inetAddress) in ipAddresses: ipAddresses.append(str(inetAddress)) return ipAddresses
def resolveIpByNsLookup(self, dnsName, dnsServer=''): """ @deprecated: Use dns_resolver.NsLookupDnsResolver(shell, dns_server=dnsServer).resolve_ips(dnsName) Resolves (or not) IP addresses by given machine name @param dnsName: the machine name to resolve IPs @type dnsName: string @param dnsServer: the dns server used @rtype: list """ resolver = dns_resolver.NsLookupDnsResolver(self.shell, dns_server=dnsServer) ipAddressList = __filterOutIpv6( __convertIpsToStrings(resolver.resolve_ips(dnsName))) return ipAddressList
def DiscoveryMain(Framework): vector = ObjectStateHolderVector() codePage = Framework.getCodePage() props = Properties() props.setProperty(BaseAgent.ENCODING, codePage) shell = None try: client = Framework.createClient(props) shell = shellutils.ShellFactory().createShell(client) dnsResolver = dns_resolver.NsLookupDnsResolver(shell) language = shell.osLanguage logger.debug("Using '%s' language bundle" % language.bundlePostfix) bundle = shellutils.getLanguageBundle('langMsCluster', language, Framework) clusterCmd = ms_cluster_discoverer.createClusterCmd(shell, bundle) if clusterCmd.isUsingCmd(): bundle = clusterCmd.detectLangBandle(Framework) clusterCmd.setBundle(bundle) vector.addAll(_discoverTopology(clusterCmd, bundle, dnsResolver)) except NoInstanceFound: errobj = errorobject.createError( errorcodes.MS_CLUSTER_INSTANCES_NOT_FOUND, None, 'MS cluster instances not found in discovery') logger.reportWarningObject(errobj) except: msg = str(sys.exc_info()[1]) logger.debugException(msg) if (msg.lower().find('timeout') > -1): errobj = errorobject.createError( errorcodes.CONNECTION_TIMEOUT_NO_PROTOCOL, None, 'Connection timed out - reactivate with larger timeout value') logger.reportErrorObject(errobj) logger.debugException('Connection timed out') else: errobj = errormessages.resolveError(msg, 'ntcmd') logger.reportErrorObject(errobj) logger.errorException(msg) try: shell and shell.closeClient() except: logger.debugException() logger.error("Unable to close shell") return vector
def getIpFromUrlObjectWithDnsList(self, dnsServers, localShell): if not self.ipAddresses: hostname = self.urlObject.getHost() if netutils.isValidIp(hostname): self.ipAddresses.append(hostname) else: for dns in dnsServers: dnsResolver = dns_resolver.NsLookupDnsResolver( localShell, dns_server=dns) inetAddresses = dnsResolver.resolve_ips_without_filter( hostname) logger.debug('find ip address: ', inetAddresses) if inetAddresses: for inetAddress in inetAddresses: if not str(inetAddress) in self.ipAddresses: self.ipAddresses.append(str(inetAddress)) return self.ipAddresses
def doDiscovery(Framework, shell, ip, credentialId, codepage, shellName, warningsList, errorsList, uduid=None): vector = ObjectStateHolderVector() try: try: languageName = shell.osLanguage.bundlePostfix langBund = Framework.getEnvironmentInformation().getBundle( 'langNetwork', languageName) remoteHostnames = dns_resolver.NsLookupDnsResolver( shell).resolve_hostnames(ip) remoteHostFqdn = None if remoteHostnames: remoteHostFqdn = remoteHostnames[0] shellObj = createShellObj(shell, ip, langBund, languageName, codepage, remoteHostFqdn) try: vector.addAll( discover(shell, shellObj, ip, langBund, Framework, uduid)) finally: # create shell OSH if connection established if shellObj and not vector.size(): hostOsh = modeling.createHostOSH(ip) shellObj.setContainer(hostOsh) vector.add(shellObj) except Exception, ex: strException = ex.getMessage() errormessages.resolveAndAddToObjectsCollections( strException, shellName, warningsList, errorsList) except: msg = str(sys.exc_info()[1]) logger.debugException('') errormessages.resolveAndAddToObjectsCollections( msg, shellName, warningsList, errorsList)
def getHostNamesFromShell(ip_string, shell=None, dnsServers=None): resolvers = [] dnsServers = dnsServers or [] try: resolvers.append(dns_resolver.SocketDnsResolver()) if shell and dnsServers: for dns in dnsServers: resolvers.append(dns_resolver.NsLookupDnsResolver(shell, dns)) dnsResolver = dns_resolver.FallbackResolver(resolvers) hostname = dnsResolver.resolve_fqdn(ip_string) aliasList = dnsResolver.resolve_hostnames(ip_string) if hostname and hostname != ip_string: logger.debug("DNS Lookup: %s: %s, %s" % (ip_string, hostname, aliasList)) return hostname, aliasList except: logger.warnException("Cannot get hostnames for %s" % ip_string) return None, []
def _resolve_ips(shell, node_names): ''' Resolves ips @types list[str] -> dict{str: list[IPAddress]} ''' name_to_ips = {} for node_name in node_names: try: resolver = dns_resolver.FallbackResolver([ dns_resolver.HostsFileDnsResolver(shell), dns_resolver.NsLookupDnsResolver(shell) ]) node_ips = resolver.resolve_ips(node_name) name_to_ips[node_name] = node_ips except: msg = sys.exc_info()[1] strmsg = '%s' % str(msg) logger.debugException('problem resolving node ', node_name, ' ', strmsg) return name_to_ips
def getIPsFromShell(hostname, shell=None, dnsServers=None): if netutils.isValidIp(hostname): return [hostname] dnsServers = dnsServers or [] resolvers = [] names = hostname.split('.', 1) if len(names) > 1: dnsServers.extend(getNameServersFromShell(names[1], shell, None)) logger.debug("lookup %s using %s" % (hostname, dnsServers)) try: resolvers.append(dns_resolver.SocketDnsResolver()) if shell and dnsServers: for dns in dnsServers: resolvers.append(dns_resolver.NsLookupDnsResolver(shell, dns)) dnsResolver = dns_resolver.FallbackResolver(resolvers) ips = dnsResolver.resolve_ips(hostname) logger.debug("DNS Lookup: %s: %s" % (hostname, ips)) return ips except: logger.warnException("Cannot get IPs for %s" % hostname) return []
def DiscoveryMain(Framework): ip = Framework.getDestinationAttribute("ip") or None port = Framework.getDestinationAttribute("https_port") or None hostCmdbId = Framework.getDestinationAttribute("host_id") or None oshvector = ObjectStateHolderVector() hostOsh = modeling.createOshByCmdbIdString('node', hostCmdbId.strip()) if not (hostOsh and ip and port): logger.error("Incorrect input data: %s" % hostCmdbId.strip()) logger.reportError("Incorrect input data") return oshvector localShell = shellutils.ShellUtils(Framework.createClient(ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME)) dnsResolver = dns_resolver.FallbackResolver([dns_resolver.SocketDnsResolver(), dns_resolver.NsLookupDnsResolver(localShell)]) hosts = dnsResolver.resolve_hostnames(ip) logger.debug("Host names to check: %s" % hosts) errors = [] discoverResult = {} for hostname in hosts: try: logger.info("Getting certificate from %s:%s" % (hostname, port)) certificates = ssl_cert_discoverer.openSslSession(hostname, port, ssl_cert_discoverer.discoverCertificates) if certificates: logger.info("Got %s certificates" % len(certificates)) discoverResult[hostname] = certificates else: logger.warn("There are no any certificates on the %s:%s" % (hostname, port)) logger.reportError("There are no any certificates on the target host") except JException,ex: logger.debugException(ex.getMessage()) errors.append(ex.getMessage()) except Exception, ex: logger.debugException(ex.message) errors.append(ex.message)