Ip address to NetBIOS/FQDN name in Java/Android

后端 未结 3 1894
梦毁少年i
梦毁少年i 2021-02-06 18:58

given the ip address of a computer on the same network of my Android device, i have to find its NetBIOS/FQDN name ... is there any \"clean\" solution to accomplish this with the

3条回答
  •  长发绾君心
    2021-02-06 19:26

    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 env = new Hashtable();
                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();
            }
        }
    }
    

提交回复
热议问题