Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
安全Java ping_Java_Ping_Processbuilder_Inetaddress - Fatal编程技术网

安全Java ping

安全Java ping,java,ping,processbuilder,inetaddress,Java,Ping,Processbuilder,Inetaddress,我有一个在局域网上ping所有计算机的项目。首先,我使用了InetAddress.isReachable(),但有时函数返回IP不可访问(),即使IP是可访问的(在windows上尝试使用内置函数,但IP是可访问的)。其次,我尝试使用以下代码: Process proc = new ProcessBuilder("ping", host).start(); int exitValue = proc.waitFor(); System.out.println("Exit Value:" + exi

我有一个在局域网上ping所有计算机的项目。首先,我使用了
InetAddress.isReachable()
,但有时函数返回IP不可访问(),即使IP是可访问的(在windows上尝试使用内置函数,但IP是可访问的)。其次,我尝试使用以下代码:

Process proc = new ProcessBuilder("ping", host).start();
int exitValue = proc.waitFor();
System.out.println("Exit Value:" + exitValue);
import java.io.*;
import java.util.*;


public class JavaPingExampleProgram
{

  public static void main(String args[]) 
  throws IOException
  {
    // create the ping command as a list of strings
    JavaPingExampleProgram ping = new JavaPingExampleProgram();
    List<String> commands = new ArrayList<String>();
    commands.add("ping");
    commands.add("-n");
    commands.add("1");
    commands.add("192.168.1.1");
    ping.doCommand(commands);
  }
  public void doCommand(List<String> command) 
  throws IOException
  {
String s = null;

ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();

BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
  System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
  System.out.println(s);
    }
  }

}
但产出是错误的。 然后我在谷歌上搜索了一下,找到了以下代码:

Process proc = new ProcessBuilder("ping", host).start();
int exitValue = proc.waitFor();
System.out.println("Exit Value:" + exitValue);
import java.io.*;
import java.util.*;


public class JavaPingExampleProgram
{

  public static void main(String args[]) 
  throws IOException
  {
    // create the ping command as a list of strings
    JavaPingExampleProgram ping = new JavaPingExampleProgram();
    List<String> commands = new ArrayList<String>();
    commands.add("ping");
    commands.add("-n");
    commands.add("1");
    commands.add("192.168.1.1");
    ping.doCommand(commands);
  }
  public void doCommand(List<String> command) 
  throws IOException
  {
String s = null;

ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();

BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
  System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
  System.out.println(s);
    }
  }

}
import java.io.*;
导入java.util.*;
公共类JavapingExample程序
{
公共静态void main(字符串参数[])
抛出IOException
{
//将ping命令创建为字符串列表
JavaPingExampleProgram ping=新的JavaPingExampleProgram();
List命令=new ArrayList();
命令。添加(“ping”);
命令。添加(“-n”);
命令。添加(“1”);
命令。添加(“192.168.1.1”);
ping.docomand(命令);
}
公共无效doCommand(列表命令)
抛出IOException
{
字符串s=null;
ProcessBuilder pb=新的ProcessBuilder(命令);
Process进程=pb.start();
BufferedReader stdInput=新的BufferedReader(新的InputStreamReader(process.getInputStream());
BufferedReader stdError=新的BufferedReader(新的InputStreamReader(process.getErrorStream());
//读取命令的输出
System.out.println(“这是命令的标准输出:\n”);
而((s=stdInput.readLine())!=null)
{
系统输出打印项次;
}
//从尝试的命令中读取任何错误
System.out.println(“这是命令的标准错误(如果有):\n”);
而((s=stdError.readLine())!=null)
{
系统输出打印项次;
}
}
}
这段代码运行良好,但问题是当Windows在其他语言上时,您无法判断地址是否可访问。你们能和我分享一个安全的方法如何在局域网或VPN网络上ping IP地址吗。谢谢。

isReachable()将使用ICMP ECHO请求(如果可以获得该权限),否则它将尝试在目标主机的端口7(ECHO)上建立TCP连接。 因此,您的问题可能是配置问题,即没有足够的权限在客户端计算机上执行此操作,或者如果您的客户端没有权限执行ICMP回显请求,则服务器上的端口7问题。可能在您的情况下,您需要解决其中一方或另一方,以使其发挥作用

我在OSX和Linux客户机上测试了以下内容,当测试其他OSX、Linux和Windows服务器机器的可达性时,它会起作用。我没有Windows计算机作为客户端运行此程序

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

public class IsReachable
{
    public static void main(final String[] args) throws IOException
    {
        final InetAddress host = InetAddress.getByName(args[0]);
        System.out.println("host.isReachable(1000) = " + host.isReachable(1000));
    }
}
如果可以获得权限,isReachable()将使用ICMP ECHO请求,否则它将尝试在目标主机的端口7(ECHO)上建立TCP连接。 因此,您的问题可能是配置问题,即没有足够的权限在客户端计算机上执行此操作,或者如果您的客户端没有权限执行ICMP回显请求,则服务器上的端口7问题。可能在您的情况下,您需要解决其中一方或另一方,以使其发挥作用

我在OSX和Linux客户机上测试了以下内容,当测试其他OSX、Linux和Windows服务器机器的可达性时,它会起作用。我没有Windows计算机作为客户端运行此程序

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

public class IsReachable
{
    public static void main(final String[] args) throws IOException
    {
        final InetAddress host = InetAddress.getByName(args[0]);
        System.out.println("host.isReachable(1000) = " + host.isReachable(1000));
    }
}

你的第一个片段怎么错了?(除了可以修改以限制计数)问题是操作系统的语言。假设语言是德语,而你不懂德语。您无法判断地址是否可访问……这不重要,重要的是返回码!第一个摘录就是这么做的。我认为语言与你的问题无关。。。如果你ping一台机器,你会得到一个特定的输出或一个错误,不管是什么语言is@PabloWindows上使用的ping函数的Lozano输出是我在IP是否可访问时释放的字符串,依此类推。问题是它返回的字符串很少,而不是一些可以放入if语句中的值。您的第一个代码段怎么会错呢?(除了可以修改以限制计数)问题是操作系统的语言。假设语言是德语,而你不懂德语。您无法判断地址是否可访问……这不重要,重要的是返回码!第一个摘录就是这么做的。我认为语言与你的问题无关。。。如果你ping一台机器,你会得到一个特定的输出或一个错误,不管是什么语言is@PabloWindows上使用的ping函数的Lozano输出是我在IP是否可访问时释放的字符串,依此类推。问题是它返回的字符串很少,而不是一些可以输入if语句的值。您的程序正在为某些计算机工作。他们中的一些人会报告说,即使他们是……你好,Zhizha,你能尝试增加超时吗。另外,如果windows尝试以管理员身份运行该程序,如果其他人尝试以root身份运行该程序,您是否可以让我知道您正在尝试哪个操作系统。。下面的链接中很少有对您有帮助的细节,谢谢您的回答@user2448155。我正在运行来自eclipse的程序(该程序仍在开发中)。操作系统是Windows7Ultimate,我以管理员身份运行eclipse(操作系统上唯一的用户是管理员)。我尝试ping的机器在Windows7下,用户是管理员。我将阅读您提供的链接。谢谢你的回答。你的程序正在为一些计算机工作。他们中的一些人会报告说,即使他们是……你好,Zhizha,你能尝试增加超时吗。另外,如果windows尝试以管理员身份运行该程序,如果其他人尝试以root身份运行该程序,您是否可以让我知道您正在尝试哪个操作系统。。下面的链接中很少有对您有帮助的细节,谢谢您的回答@user2448155。我正在运行来自eclipse的程序(该程序仍在开发中)。操作系统是Windows7Ultimate,我以管理员身份运行eclipse(操作系统上唯一的用户是管理员)。我尝试ping的机器在Windows7下,用户是管理员。我将阅读您提供的链接。谢谢你的回答。