java - php checkdnsrr java等价物

标签 java php dns

是否有 PHP 函数 checkdnsrr 的 Java 版本?

最佳答案

如果您只需要任何 IP 地址,您可以使用 InetAddress(虽然 API 没有指定这将匹配什么类型的主机):

InetAddress[] addresses = java.net.InetAddress.getAllByName(String host)

然后,您可以对 IP 地址的原始字节使用 getAddress(),对 IP 地址的格式化字符串表示形式使用 getHostAddress()。

如果您正在寻找特定类型的记录(例如 MX 记录),这是我在 this webpage 上找到的片段

import  java.util.Hashtable;
import  javax.naming.*;
import  javax.naming.directory.*;

public class MXLookup {
  public static void main( String args[] ) {
    if( args.length == 0 ) {
      System.err.println( "Usage: MXLookup host [...]" );
      System.exit( 99 );
    }
    for( int i = 0; i < args.length; i++ ) {
      try {
        System.out.println( args[i] + " has " +
          doLookup( args[i] ) + " mail servers" );
      }
      catch( Exception e ) {
        System.out.println(args[i] + " : " + e.getMessage());
      }
    }
  }

  static int doLookup( String hostName ) throws NamingException {
    Hashtable env = new Hashtable();
    env.put("java.naming.factory.initial",
            "com.sun.jndi.dns.DnsContextFactory");
    DirContext ictx = new InitialDirContext( env );
    Attributes attrs = 
       ictx.getAttributes( hostName, new String[] { "MX" });
    Attribute attr = attrs.get( "MX" );
    if( attr == null ) return( 0 );
    return( attr.size() );
  }
}

关于java - php checkdnsrr java等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2474123/

相关文章:

java - 我用 Hibernate/JPA 得到一个 "error, string or binary data would be truncated"

java - Tomcat 7 + JNI : UnsatisfiedLinkError: cannot open shared object file: No such file or directory

php - 为什么这个 PDO 语句会默默地失败?

php - SQL 查询(在两个表之间删除)

Python Linux DNS 别名和地址回复

java - 使用JAVA在HIVE中创建表

java - 如何编码 "if input != a number, then..."

php - Node.js 与 PHP

linux - Node.js 部署到 Web

azure - 将特定 Web 应用服务路径映射到自定义域