简体   繁体   English

在java中查找给定IP地址或MAC地址的操作系统名称,操作系统名称

[英]Find Computer Name,Operating System name of a given IP address or MAC address In java

Is there a way to find : 有没有办法找到:

  1. computer name 电脑名称
  2. Operating system 操作系统
  3. hardware type(laptop or desktop) 硬件类型(笔记本电脑或台式机)

Of a give IP or MAC address in java. 用java给出IP或MAC地址。 I've been give a task to get that data. 我一直在负责获取数据。 I'd really appreciate a code sample or if you know of any libraries that can be used. 我真的很感谢代码示例,或者如果您知道可以使用的任何库。 eg : 例如:

If I were to use an IP address of a computer on the same network. 如果要使用同一网络上计算机的IP地址。 I would like to know the name of the computer and which OS they are using and other hardware metadata if that's possible. 我想知道计算机的名称,他们正在使用的操作系统以及其他硬件元数据(如果可能)。

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

I guess you need the following properties: 我想您需要以下属性:

COMPUTERNAME
OS

regarding MAC Addrss and IP Address you have 2 options: 关于MAC地址和IP地址,您有2个选择:

use 采用

InetAddress ip;
  try {

    ip = InetAddress.getLocalHost();
    System.out.println("Current IP address : " + ip.getHostAddress());

  } catch (UnknownHostException e) {

    e.e.printStackTrace();

  } 

or use ipconfig/ifconfig output but I'll leave the choice it to you 或使用ipconfig / ifconfig输出,但我将选择权留给您

Getting the mac 获取Mac

The proper way to do this is to use the ARP protocol, but that resides at the Data Link Layer to which Java provides no direct API access. 正确的方法是使用ARP协议,但这位于Java不提供直接API访问权限的数据链路层 So you'd have to go native for a good solution. 因此,您必须本地化才能找到一个好的解决方案。 Use jNetPcap to gain access to this. 使用jNetPcap可以访问此文件。

Getting computer name and OS 获取计算机名称和操作系统

This would require you to tap into some protocol that provides that data. 这将需要您使用提供该数据的某种协议。 NBT provides this kind of data. NBT提供此类数据。

If you're trying to find out the hostname associated with an IP address, use InetAddress#getHostName() , though that will only work if the local network has hosts registered for reverse DNS. 如果您尝试查找与IP地址关联的主机名,请使用InetAddress#getHostName() ,尽管只有在本地网络中注册了用于反向DNS的主机时,该方法才有效。 Finding out another host's OS requires either a service you run that tells you or inference through fingerprinting , which isn't available from Java and isn't entirely reliable. 要找到另一台主机的操作系统,您需要运行一项可以告诉您的服务,或者需要通过指纹进行推断 ,而指纹是Java无法提供的,也不是完全可靠的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM