【问题标题】:Retrieving a MAC address from a PING sweep从 PING 扫描中检索 MAC 地址
【发布时间】:2013-12-09 23:06:39
【问题描述】:

我开发了一个代码,使我能够 ping 一个 IP 地址范围。我的 ping 扫描的结果确定了哪些本地机器可以访问,如果可以访问那里的主机名,以及它们是否无法访问。

目前在检索可访问 IP 地址的 MAC 地址时遇到问题。有没有人对此有解决方案?

package networkping;

import java.io.IOException;
import java.net.InetAddress;

/**
*
* @author Learner
*/
public class Networkping {


public static void main(String[] args) throws IOException {

    InetAddress localhost = InetAddress.getLocalHost();
    // this code assumes IPv4 is used
    byte[] ip = localhost.getAddress();

    for (int i = 1; i <= 254; i++)
    {
        ip[3] = (byte)i;
        InetAddress address = InetAddress.getByAddress(ip);
        if (address.isReachable(1000))
        {
            System.out.println(address + " Address is reachable" );

        }
        else if (!address.getHostAddress().equals(address.getHostName()))
        {
            System.out.println(address + " Address is known in a DNS lookup and is reachable ");
        }
        else
        {
            System.out.println(address + " Address is unreachable");
        }
    }

}

谢谢

【问题讨论】:

  • 遗憾的是,如果没有 JNI,Java 无法做到这一点,并且取决于网络配置/操作系统,可能根本不会。
  • @hexafraction 感谢您的评论和意见,但我一直将此作为作业,导师向我保证可以这样做
  • @user3071069 看看我回答中的链接。这是可能的,但你需要绕道而行。

标签: java ping ethernet


【解决方案1】:

你不能只使用 java 来做到这一点。

有两种选择:

  • 通过 java 执行另一个进程并通过标准输出与您的原始 java 应用程序进行通信。比如这样的进程是ARP
  • 按照 cmets 中的建议使用 JNI。

还请查看以下资源/答案:

第二个资源甚至有一个实现的方法调用名为private String getMac(String ip)的ARP

【讨论】:

    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多