Ip address to NetBIOS/FQDN name in Java/Android

可紊 提交于 2019-12-03 05:10:53
Tom

You can use JCIFS open source library.

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

works both ways, ip address to hostname and vice versa.

Actually, the code provided by Tom does not work, this code works for me (with JCIFS lib.)

NbtAddress[] nbts = NbtAddress.getAllByAddress("IP ADDRESS AS STRING");
String netbiosname = nbts[0].getHostName();

returns NetBios device name as string if successful or throws UnknownHostException if target does not exist or has no NetBios name.

Try this...

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();
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!