Reputation: 1031
I am using this part of code to ping an ip address in java but only pinging localhost is successful and for the other hosts the program says the host is unreachable. I disabled my firewall but still having this problem
public static void main(String[] args) throws UnknownHostException, IOException {
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
ipAddress = "173.194.32.38";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
The output is:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is NOT reachable
Upvotes: 96
Views: 311564
Reputation: 72001
You can use java in a shell script, without compiling it (java does this on the fly) and run a "ping" type test for any host/port. This takes 2 arguments, the host and the port. Basic idea comes from this answer
e.g.
# test if host and port can be opened from java
$ test_host_and_port myhost 443
#!/usr/bin/java --source 11
import java.net.InetAddress;
import java.net.Socket;
public class PingExample {
public static void main(String[] args){
try{
boolean result = serverListening(args[0], Integer.valueOf(args[1]));
System.out.println("Is host reachable? " + args[0] + ", " + result);
} catch (Exception e){
e.printStackTrace();
}
}
public static boolean serverListening(String host, int port)
{
Socket s = null;
try
{
s = new Socket(host, port);
return true;
}
catch (Exception e)
{
return false;
}
finally
{
if(s != null)
try {s.close();}
catch(Exception e){}
}
}
}
Upvotes: 0
Reputation: 131
I prefer to this way:
/**
*
* @param host
* @return true means ping success,false means ping fail.
* @throws IOException
* @throws InterruptedException
*/
private static boolean ping(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
return proc.waitFor(200, TimeUnit.MILLISECONDS);
}
This way can limit the blocking time to the specific time,such as 200 ms.
It works well in MacOS、Android and Windows, and should used in JDK 1.8.
This idea comes from Mohammad Banisaeid,but I can't comment. (You must have 50 reputation to comment)
Upvotes: 1
Reputation: 135
short recommendation: don't use isReachable(), call the system ping, as proposed in some of the answers above.
long explanation:
Upvotes: 13
Reputation: 2626
You can use this method to ping hosts on Windows and other platforms:
private static boolean ping(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
int returnVal = proc.waitFor();
return returnVal == 0;
}
Upvotes: 15
Reputation: 26
I tried a couple of options:
InetAddress.getByName(ipAddress)
, the network on windows started misbehaving after trying a couple of times
Java HttpURLConnection
URL siteURL = new URL(url);
connection = (HttpURLConnection) siteURL.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(pingTime);
connection.connect();
code = connection.getResponseCode();
if (code == 200) {
code = 200;
}.
This was reliable but a bit slow
I finally settled to creating a batch file on my windows machine with the following contents: ping.exe -n %echoCount% %pingIp%
Then I called the .bat file in my java code using
public int pingBat(Network network) {
ProcessBuilder pb = new ProcessBuilder(pingBatLocation);
Map<String, String> env = pb.environment();
env.put(
"echoCount", noOfPings + "");
env.put(
"pingIp", pingIp);
File outputFile = new File(outputFileLocation);
File errorFile = new File(errorFileLocation);
pb.redirectOutput(outputFile);
pb.redirectError(errorFile);
Process process;
try {
process = pb.start();
process.waitFor();
String finalOutput = printFile(outputFile);
if (finalOutput != null && finalOutput.toLowerCase().contains("reply from")) {
return 200;
} else {
return 202;
}
} catch (IOException e) {
log.debug(e.getMessage());
return 203;
} catch (InterruptedException e) {
log.debug(e.getMessage());
return 204;
}
}
This proved to be the fastest and most reliable way
Upvotes: 0
Reputation: 1215
Even though this does not rely on ICMP on Windows, this implementation works pretty well with the new Duration API
public static Duration ping(String host) {
Instant startTime = Instant.now();
try {
InetAddress address = InetAddress.getByName(host);
if (address.isReachable(1000)) {
return Duration.between(startTime, Instant.now());
}
} catch (IOException e) {
// Host not available, nothing to do here
}
return Duration.ofDays(1);
}
Upvotes: 3
Reputation: 39
InetAddress is not always return correct value. It is successful in case of Local Host but for other hosts this shows that the host is unreachable. Try using ping command as given below.
try {
String cmd = "cmd /C ping -n 1 " + ip + " | find \"TTL\"";
Process myProcess = Runtime.getRuntime().exec(cmd);
myProcess.waitFor();
if(myProcess.exitValue() == 0) {
return true;
}
else {
return false;
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
Upvotes: 0
Reputation: 19195
Here is a method for pinging an IP address in Java
that should work on Windows
and Unix
systems:
import org.apache.commons.lang3.SystemUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CommandLine
{
/**
* @param ipAddress The internet protocol address to ping
* @return True if the address is responsive, false otherwise
*/
public static boolean isReachable(String ipAddress) throws IOException
{
List<String> command = buildCommand(ipAddress);
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
try (BufferedReader standardOutput = new BufferedReader(new InputStreamReader(process.getInputStream())))
{
String outputLine;
while ((outputLine = standardOutput.readLine()) != null)
{
// Picks up Windows and Unix unreachable hosts
if (outputLine.toLowerCase().contains("destination host unreachable"))
{
return false;
}
}
}
return true;
}
private static List<String> buildCommand(String ipAddress)
{
List<String> command = new ArrayList<>();
command.add("ping");
if (SystemUtils.IS_OS_WINDOWS)
{
command.add("-n");
} else if (SystemUtils.IS_OS_UNIX)
{
command.add("-c");
} else
{
throw new UnsupportedOperationException("Unsupported operating system");
}
command.add("1");
command.add(ipAddress);
return command;
}
}
Make sure to add Apache Commons Lang
to your dependencies.
Upvotes: 1
Reputation: 1
This should work:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Pinger {
private static String keyWordTolookFor = "average";
public Pinger() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
//Test the ping method on Windows.
System.out.println(ping("192.168.0.1")); }
public String ping(String IP) {
try {
String line;
Process p = Runtime.getRuntime().exec("ping -n 1 " + IP);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (((line = input.readLine()) != null)) {
if (line.toLowerCase().indexOf(keyWordTolookFor.toLowerCase()) != -1) {
String delims = "[ ]+";
String[] tokens = line.split(delims);
return tokens[tokens.length - 1];
}
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
return "Offline";
}
}
Upvotes: -3
Reputation: 967
Just an addition to what others have given, even though they work well but in some cases if internet is slow or some unknown network problem exists, some of the codes won't work (isReachable()
). But this code mentioned below creates a process which acts as a command line ping (cmd ping) to windows. It works for me in all cases, tried and tested.
Code :-
public class JavaPingApp {
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 = "stackoverflow.com"; //Any IP Address on your network / Web
runSystemCommand("ping " + ip);
}
}
Hope it helps, Cheers!!!
Upvotes: 3
Reputation: 457
I think this code will help you:
public class PingExample {
public static void main(String[] args){
try{
InetAddress address = InetAddress.getByName("192.168.1.103");
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e){
e.printStackTrace();
}
}
}
Upvotes: 44
Reputation: 2614
InetAddress.isReachable()
according to javadoc:
".. A typical implementation 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..".
Option #1 (ICMP) usually requires administrative (root)
rights.
Upvotes: 78
Reputation: 109
I know this has been answered with previous entries, but for anyone else that comes to this question, I did find a way that did not require having use the "ping" process in windows and then scrubbing the output.
What I did was use JNA to invoke Window's IP helper library to do an ICMP echo
See my own answer to my own similar issue
Upvotes: 0
Reputation: 122
On linux with oracle-jdk the code the OP submitted uses port 7 when not root and ICMP when root. It does do a real ICMP echo request when run as root as the documentation specifies.
If you running this on a MS machine you may have to run the app as administrator to get the ICMP behaviour.
Upvotes: 1
Reputation: 511
It will work for sure
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("-c");
commands.add("5");
commands.add("74.125.236.73");
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);
}
}
}
Upvotes: 12
Reputation: 1641
You can not simply ping in Java as it relies on ICMP, which is sadly not supported in Java
http://mindprod.com/jgloss/ping.html
Use sockets instead
Hope it helps
Upvotes: 20
Reputation: 20059
Check your connectivity. On my Computer this prints REACHABLE for both IP's:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is reachable
EDIT:
You could try modifying the code to use getByAddress() to obtain the address:
public static void main(String[] args) throws UnknownHostException, IOException {
InetAddress inet;
inet = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
inet = InetAddress.getByAddress(new byte[] { (byte) 173, (byte) 194, 32, 38 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
The getByName() methods may attempt some kind of reverse DNS lookup which may not be possible on your machine, getByAddress() might bypass that.
Upvotes: 21