Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a remote system is reachable

Tags:

I want to test whether a remote system is reachable using Java or in other words "send a ping" using Java. This functionality should be encapsulated in a method with boolean value, for example

public boolean isReachable(String ip) {    // what to do :-) } 

I've tested the Java Process class, but I don't think that it is the best way to do this, because of the complex output handling with OutputBuffers.

Process proc = Runtime.getRuntime().exec("ping " + ip); 

Another possibility would be creating a Socket Connection and handle thrown exceptions, but if the remote system is a "naked" unix system, there might be no Socket on the other side :-) Additionally, I'd like to be able to set a timeout, when a remote system is not reachable.

So how could I do this? Thank you!

like image 294
strauberry Avatar asked Oct 07 '11 09:10

strauberry


People also ask

How do I know if my remote port is accessible?

Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status. If the port is open, only a cursor will show.

How do I know if a host is reachable Linux?

The ping command is a simple network utility command-line tool in Linux. It's a handy tool for quickly checking a host's network connectivity. It works by sending an ICMP message ECHO_REQUEST to the target host. If the host reply with ECHO_REPLY, then we can safely conclude that the host is available.

Which command can test if a port on a remote computer is open?

One of the biggest perks of Telnet is with a simple command you can test whether a port is open. Issuing the Telnet command telnet [domainname or ip] [port] will allow you to test connectivity to a remote host on the given port.


1 Answers

InetAddress.getByName(ip).isReachable(timeout); 
like image 72
andypandy Avatar answered Oct 15 '22 15:10

andypandy