gpt4 book ai didi

java - Java中ICMP请求需要获取哪些权限?

转载 作者:太空狗 更新时间:2023-10-29 11:18:54 24 4
gpt4 key购买 nike

我想通过 Java ping 一个 IP。当我从命令行 ping 并跟踪网络包时,我看到了 ICMP 请求。当我从我的应用程序 ping 并跟踪网络包时,我在端口 7 看到 TCP 请求。

我已经检查了InetAddress.isReachable()的文档:

Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible.

A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

我可以使用运行我的 Java 应用程序的用户来运行 sudo 所需的命令。

为了我的目的应该获得哪种特权?我应该检查什么?

PS:我在 stackoverflow.com 上问过这个问题,因为它与 Java 问题(Java 源代码需要运行)比系统问题更相关。

最佳答案

你问的是 Linux 还是 Java? - 显然,运行程序的 JVM 进程需要拥有打开“原始”ICMP 套接字所需的所有 Linux 特权,就像任何其他进程一样。

对于一种实现,请参见例如Java_java_net_Inet4AddressImpl_isReachable0基本上可以归结为:

/*
* Let's try to create a RAW socket to send ICMP packets
* This usually requires "root" privileges, so it's likely to fail.
*/
fd = JVM_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (fd != -1) {
/*
* It didn't fail, so we can use ICMP_ECHO requests.
*/
return ping4(env, fd, &him, timeout, netif, ttl);
}

/*
* Can't create a raw socket, so let's try a TCP socket
*/
...

关于java - Java中ICMP请求需要获取哪些权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26971908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com