Created
March 18, 2013 17:18
-
-
Save shanet/5188968 to your computer and use it in GitHub Desktop.
A quick Java program to check if a server is up or a service on a server is up.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetAddress; | |
import java.net.Socket; | |
import java.io.IOException; | |
import java.net.UnknownHostException; | |
public class Monitor { | |
public static void main(String[] args) { | |
if(args.length < 2) { | |
System.out.println("Bad number of arguments."); | |
return; | |
} | |
String server = args[0]; | |
int port = Integer.valueOf(args[1]); | |
try { | |
InetAddress ina = InetAddress.getByName(server); | |
if(ina.isReachable(3000)) { | |
System.out.println("Server is up."); | |
} else { | |
System.out.println("Server is down!"); | |
return; | |
} | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
return; | |
} | |
Socket socket = null; | |
try { | |
socket = new Socket(server, port); | |
if(socket.isConnected()) { | |
System.out.println("Service is up on port " + port + "."); | |
} else { | |
System.out.println("Service is down on port " + port + "!"); | |
} | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} finally { | |
if(socket != null && socket.isConnected()) { | |
try { | |
socket.close(); | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
} | |
} | |
return; | |
} | |
} |
Not surprising, this is from seven years ago.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working