Search code examples
javascaladnsnslookup

Scala or java lib to resolve host name with specific nameserver


Given a hostname like "example.com" I want to get its IP-addreses using specific nameserver.

java.net.InetAddress has

 private static InetAddress getByName(String host, InetAddress reqAddr)

which looks like what I need, but the blooper is that method is private.

https://github.com/gilt/scala-srv-dns has

def lookup(serviceName: ServiceName,
         transportProtocol: TransportProtocol,
         dnsSearchPaths: List[String]): Seq[ServiceRecord]

But, again, private method.

So, what lib can I use? And do you have any idea why the libs listed above have these methods private?


Solution

  • dnsjava was the solution for me (http://www.dnsjava.org/)

    def lookup(host: String, nameServer: String): Array[String] = {
      val l = new Lookup(host)
      l.setResolver(new SimpleResolver(nameServer))
      l.run()
      if (l.getResult() == Lookup.SUCCESSFUL)
        l.getAnswers().map(_.rdataToString())
      else
        Array.empty[String]
    }