1

I have a project that pings all computera on LAN network. First I used InetAddress.isReachable() but sometimes function returns that IP is not reachable() even if IP is reachable(tried with build in function on windows and IP was reachable) . Second i tried with this code:

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

But output is wrong. Then I googled a bit and find out this code:

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);
    }
  }

}

This code worked fine but problem is when Windows is on other langauge you can't tell if address is reachable or not. Can you guys share with me a safe method how to ping IP addresses on LAN or VPN network. Thank you.

ZhiZha
  • 143
  • 2
  • 4
  • 13
  • How is your first snippet wrong? (apart that it could be modified to limit the count) – fge Jun 07 '13 at 10:43
  • Problem is langauge of OS. Let's say that language is German and you don't know German Language. You can't tell is address is reachable or not.... – ZhiZha Jun 07 '13 at 10:50
  • That should NOT matter, what matters is the return code! And the first extract does exactly that. – fge Jun 07 '13 at 10:56
  • I think language has nothing to do with your problem... if you ping a machine you'll get a certain output or an error, whatever the language is – Pablo Lozano Jun 07 '13 at 11:31
  • @Pablo Lozano output of ping function used on Windows is String that i desplay if IP is reachable or not and so on. Problem is that it return few strings not some value that you can put in if statement. – ZhiZha Jun 08 '13 at 14:18

1 Answers1

1

isReachable() 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. Thus your problem is probably a configuration issue of not enough permissions to do this on the client machine or a port 7 issue on the server if your client doesn't have permission to do the ICMP ECHO REQUEST. Probably both in your case, you need to resolve one side or the other to get this to work.

I tested the following on OSX and Linux clients and it works when testing for reachablity of other OSX, Linux and Windows Server machines. I don't have a Windows machine to run this as a client.

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));
    }
}
Shivakumar ss
  • 653
  • 7
  • 19
  • Your program is working for some computers. Some of them will report that they are not reachable even if they are... – ZhiZha Jun 07 '13 at 10:49
  • Hello Zhizha, Can you try increasing the timeout. also can you let me know on which os this program are you trying, if windows try running the program as administrator and if it is others try as root ? .. There are few details which will be helpful for you in this below link http://bordet.blogspot.in/2006/07/icmp-and-inetaddressisreachable.html – Shivakumar ss Jun 07 '13 at 17:01
  • Thank you for your answer @user2448155. I am running program from eclipse (program is still under development). OS is Windows 7 Ultimate and I am running eclipse as administrator (only user on OS is administrator). Machine that I am tring to ping is under Windows 7 and user is administrator. I will read link that you provided. Thank you for your answer. – ZhiZha Jun 08 '13 at 14:14