Ping function returns that all pinged IP addresses is reachable

走远了吗. 提交于 2020-01-07 04:45:08

问题


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)


回答1:


Use isReachable() instead.

InetAddress.getByName(address).isReachable(timeout);



回答2:


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)




回答3:


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



来源:https://stackoverflow.com/questions/16972206/ping-function-returns-that-all-pinged-ip-addresses-is-reachable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!