Skip to content

Instantly share code, notes, and snippets.

@madan712
Last active December 19, 2023 06:57
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save madan712/4509039 to your computer and use it in GitHub Desktop.
Save madan712/4509039 to your computer and use it in GitHub Desktop.
Java program to ping an IP address
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PingIP {
public static void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String ip = "google.com";
runSystemCommand("ping " + ip);
}
}
@rush2sk8
Copy link

This was exactly what i needed thanks so much

@karthik996
Copy link

thanks dude

@s-buch
Copy link

s-buch commented Apr 24, 2015

Thanks

@LindaKade
Copy link

Thanks.

@NemoClown
Copy link

Thank you very much.

@Naveedali1234
Copy link

how can we ping ipv4 addresses and ipv6 addresses ?? if you can help me it would be really appreciated.

thank you

@shivanshm4
Copy link

can you please explain what does that object p of Process class does.. I really cant get that part

@JuanchoSL
Copy link

@shivanshm4 the p Object is the process, and contain the console response of the command execution, readable with the next lines of the code

@LearnItCodeIt
Copy link

I do not uneerstand the logic of the syntax can some one please explain for me on my private email. My email address is:
(xcoder.learning@gmail.com)

@kusHELL
Copy link

kusHELL commented Dec 3, 2016

nice job dude...#madan712....just remove public while declaring class....it gives error...rest whole code is awesome

@deepnighttwo
Copy link

Cool and great useful

@Acvrock
Copy link

Acvrock commented Jan 18, 2017

The ping -c parameter is required on some systems

@VishalGurung
Copy link

Hey nice one

@sisgandarli
Copy link

I would do it in a different way:

public static void isReachable(String address) {
        try {
            InetAddress inetAddress = InetAddress.getByName(address);
            boolean isReachable = inetAddress.isReachable(5000);
            System.out.printf("Is the address [%s] reachable? -%s\n", address, isReachable ? "Yes" : "No");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

@ankovi
Copy link

ankovi commented Nov 8, 2017

@QuercusNemus
Copy link

Hello, awesome code!
Though i tried to expand the usage a lite and made it possible to set the number of pings and size on the package sent. But with only an input stream there is no way to cancel the ping (Ctrl+C) if you want it to stop?

The problem is when i implemented the option to have a continues ping that will run until you tell it not to. And that ping will continue even after you close the java application.

Do you know if there is a way to send a stop command the ping process and then read the result from that ping?

@kalingarajs
Copy link

what is the main usage of ping in java

@kalingarajs
Copy link

anyone is available are not......answer sollunga da

@fidobyigero
Copy link

fidobyigero commented Jan 20, 2018

Awesome!
I struggled alot with NetworkInterfaces but couldn't help.
Thanks so much, it works fine.

@ArielMannes
Copy link

thanks !

@f5killer
Copy link

f5killer commented Jun 6, 2018

Hi JavaExpert, I am new member in this group. I would like to add bulk ping, which will take from file and generate output in CSV format if 3 consecutive ping successful. I am sure you guys will help me in writing program. Appreciate your help

@nguyenlinhnttu
Copy link

@EAndresen You can try p.destroy(); to stop ping.

@yudistira000
Copy link

how to use that code in java jframe?

@auto-1
Copy link

auto-1 commented Feb 22, 2020

how to use that code in java jframe?

Exact same method, you'd just need to handle an event that triggers it and obviously the response. Let me know if you need more info!

@auto-1
Copy link

auto-1 commented Feb 22, 2020

This works fine on Mac OS, however is there an additional command that needs to be called for Windows systems? Seems to fail on Windows.

@xtrecoolx
Copy link

I would do it in a different way:

public static void isReachable(String address) {
        try {
            InetAddress inetAddress = InetAddress.getByName(address);
            boolean isReachable = inetAddress.isReachable(5000);
            System.out.printf("Is the address [%s] reachable? -%s\n", address, isReachable ? "Yes" : "No");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

This is ICMP way. Unfortunately, it is not always available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment