Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android java /system/bin/ping always returns exitCode 2, why?

I'm trying to use the /system/bin/ping command on Android to ping the devices on my network. For some reason, it returns exit code 2 for every ping I try. I've tried:www.google.com, 192.168.2.1(router local ip), and currently every possible ip for the devices on the subnet... but it always returns 2. Here's my code.

onCreate

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            String subnet = "192.168.2";
            for (int i = 0; i <= 255; i++) {
                String node = subnet + "." + i;
                System.out.println(pingHost(node));
            }

        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }

    }

pingHost()

public static int pingHost(String host) throws IOException, InterruptedException {
        String cmd = "/system/bin/ping -c 1 -W 1000 " + host;
        Process proc = Runtime.getRuntime().exec(cmd);
        proc.waitFor();
        return proc.exitValue();
    }

I've tried with and without a timeout... still the only exit code that prints is 2.

Any ideas?

like image 632
God Usopp Avatar asked Oct 20 '25 14:10

God Usopp


1 Answers

I printed out the error stream and it told me I needed INTERNET permissions to use ping. I added the INTERNET permissions to my manifest and voila!

Thank you Codebender for the suggestion!

like image 83
God Usopp Avatar answered Oct 22 '25 02:10

God Usopp