0

I am tring to ping IP addresses from 192.168.1.1 to 192.168.1.254. First I was using I InetAddress class but it was bugged and some IPs where not reachable even if they are. After that I tried this method and it worked very well for single ping IP but when I put it inside for-loop all pinged IPs where reachable... Can you guys tell me what's wrong here?

CODE:

public class Main {
public static void main(String[] args) {
    String ip="192.168.1.";
    try
    {
        for(int i=0;i<=254;i++){
        String ip2=ip+i;
        boolean reachable = (java.lang.Runtime.getRuntime().exec("ping -n 1 "+ip2).waitFor()==0);
        if(reachable){
            System.out.println("IP is reachable:: "+ip2);
        }
        else{
            System.out.println("IP is not reachable: "+ip2);
        }
        }
    }catch(Exception e)
    {
        e.printStackTrace();
    }

}

}

EDIT 1:

I used built in Java function to preform pinging but it's not working (again)

here is code that I used

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test {

public static void main(String[] args) throws UnknownHostException, IOException {
    String ip = "192.168.1.243";
    InetAddress inet = InetAddress.getByName(ip);
    System.out.println("Sending Ping Request to " + ip);
    if (inet.isReachable(5000)){
        System.out.println(ip+" is reachable");
    }
    else{
        System.out.println(ip+" is not reachable");
    }

}
}

OUTPUT IS:

Sending Ping Request to 192.168.1.243
192.168.1.243 is not reachable

Also here is ping result when I do pinging from Windows 7 built in Ping function (cmd)

enter image description here

ZhiZha
  • 143
  • 2
  • 4
  • 13
  • You're checking if `waitFor()==0`. This means you only think it's reachable if it returns immediately (for some value of immediately). What if it returns in 0.245 seconds? In other words, you want to check what ping returns, not whether or not it does. I should note that using Java to call ping is a terrible idea – Chris Jun 06 '13 at 21:13
  • .waitFor returns the exit status of the process, does not the ping command always return 0 no matter if the ping itself was successful or not? http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html#waitFor%28%29 – Simon Forsberg Jun 06 '13 at 21:14
  • @Chris calling .waitFor will cause the current thread to block if necessary, waiting for the process to end, as stated in the docs "causes the current thread to wait, if necessary, until the process represented by this Process object has terminated." – Simon Forsberg Jun 06 '13 at 21:15
  • Since it's my school project I need to code it in Java. I do know that pinging in Java is bad idea but I don't have any other options.... – ZhiZha Jun 06 '13 at 21:15
  • @SimonAndréForsberg Right. I guess I misinterpreted the output of waitFor. Based on the command line params the author is using ping on Windows, which as you point out, always returns 0. – Chris Jun 06 '13 at 21:17
  • @Chris If you do this code with only one IP result will be correct but if you do with more ips (like me) output will be buggy.... – ZhiZha Jun 06 '13 at 21:20
  • @ZhiZha What do you mean "will be buggy"? – Chris Jun 06 '13 at 21:24
  • @Chris I just tried your code and for me it is always "buggy". Try with one single non-reachable ip without a loop. For me it says the ip is reachable, even though it is not. – Simon Forsberg Jun 06 '13 at 21:26
  • Well I noticed that this way of pinging is bad. I will try with streams and will post back if I got any success. – ZhiZha Jun 06 '13 at 21:54

3 Answers3

3

Use isReachable() instead.

InetAddress.getByName(address).isReachable(timeout);
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
  • isReachable() is buggy. It will return false for some IPs even if they are reachable... – ZhiZha Jun 06 '13 at 21:40
  • @ZhiZha I really hope you put a long enough value for the `timeout`. This seems like the best solution for me. I tried it and it seems to work fine. I used a timeout value of 5000. – Simon Forsberg Jun 06 '13 at 21:58
  • @Simon André Forsberg InetAddress.getByName(address).isReachable(timeout); can't use that because sometimes results are wrong. – ZhiZha Jun 06 '13 at 22:30
  • @ZhiZha What value of timeout are you using? Show me a code example when you are using this. Also show me the output from when you execute the ping command for the same ip without java. – Simon Forsberg Jun 07 '13 at 11:29
  • @Simon André Forsberg I added code to my original (first) post. There you will find Java code that perform pinging and result when I perform pinging with Windows Ping function. – ZhiZha Jun 08 '13 at 16:54
1

Why it does not work:

You are using the exit status of the ping process, not the actual result of the ping itself. It will just tell you whether or not the process exited normally or not. A failed ping does not cause the process to exit abnormally, and thus an exit code of 0 (zero) is always returned.

What you could try instead:

Get the output stream of the process, which will tell you what the output is. Then try to interpret/parse this however you would like:

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html#getOutputStream%28%29

(Although this also doesn't feel perfect for me, it is a much better choice than using the exit code)

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • Thank you for help. Will try to use streams and check if string contains unreachable and by that I will check if I IP is reachable or not... – ZhiZha Jun 06 '13 at 21:51
  • @ZhiZha It doesn't have to say 'unreachable', it can also say `Request timed out.` Also, what it will say is a matter of the language settings of the computer the program is running on, which is why this also doesn't feel perfect for me. – Simon Forsberg Jun 06 '13 at 21:55
  • What do you suggest? Is there any good way to do a simple ping? – ZhiZha Jun 06 '13 at 21:59
  • @ZhiZha I strongly suggest using the answer by BlueRaja, that is: `InetAddress.getByName(address).isReachable(5000);` for a timeout for 5 seconds. According to the documentation at http://docs.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29 , isReachable uses ICMP (same as ping) and can also use other ways to try to reach the destination. – Simon Forsberg Jun 06 '13 at 22:03
  • Problem is that InetAddress.getByName(address).isReachable(5000) returns false even IP is reachable for some IPs... – ZhiZha Jun 06 '13 at 22:14
0

Dear sir The issue why you program cannot ping systems over the loop is, the execution of the loop is faster compared to the replies from the systems.That is why some of them replies Unreachable, in order to solve such problem you are supposed to use a thread and introduce a little delay on each ping by using Thread.sleep() method.I think this will work Thank you