IP地址到Java / Android中的NetBIOS / FQDN名称

时间:2012-10-07 10:13:49

标签: java android hostname fqdn netbios

考虑到我的Android设备的同一网络上的计算机的IP地址,我必须找到它的NetBIOS / FQDN名称...是否有任何“干净”的解决方案来完成这个Android SDK和一般来说在java ?

显然InetAddress.get * HostName不返回NetBIOS名称:)

2 个答案:

答案 0 :(得分:5)

您可以使用JCIFS开源库。

 InetAddress addr = NbtAddress.getByName( "hostname" ).getInetAddress();

两种方式,ip地址到主机名,反之亦然。

答案 1 :(得分:0)

试试这个......

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class SearchNetBIOSName {

    public static void main(String[] args) {
        try {
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://my.domain.com:389");
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, "cn=administrator,cn=users,dc=my,dc=domain,dc=com");
            env.put(Context.SECURITY_CREDENTIALS, "********");
            LdapContext context = new InitialLdapContext(env, null);
            String searchBase = "cn=Partitions,cn=Configuration,dc=my,dc=domain,dc=com";
            String searchFilter = "(&(objectcategory=Crossref)(netbiosname=*))";
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
            NamingEnumeration answers = context.search(searchBase, searchFilter, controls);
            while (answers.hasMore()) {
                SearchResult rs = (SearchResult) answers.next();
                String netBiosName = rs.getAttributes().get("NetBIOSName").get(0).toString();
                System.out.println(netBiosName);
            }
            context.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}