import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NetworkDiagnostics{
private final String os = System.getProperty("os.name").toLowerCase();
public String traceRoute(InetAddress address){
String route = "";
try {
Process traceRt;
if(os.contains("win")) traceRt = Runtime.getRuntime().exec("tracert " + address.getHostAddress());
else traceRt = Runtime.getRuntime().exec("traceroute " + address.getHostAddress());
// read the output from the command
route = convertStreamToString(traceRt.getInputStream());
// read any errors from the attempted command
String errors = convertStreamToString(traceRt.getErrorStream());
}
catch (IOException e) {
}
return route;
}
private String convertStreamToString(InputStream inputStream) {
BufferedReader bf=new BufferedReader(new InputStreamReader(inputStream));
String line="";
try {
while((line=bf.readLine())!=null){
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String args[]){
try {
InetAddress ip=InetAddress.getByName(args[0]);
NetworkDiagnostics networkDiagnostics=new NetworkDiagnostics();
networkDiagnostics.traceRoute(ip);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Compile it as >javac NetworkDiagnostics.java
Run it as >java NetworkDiagnostics example.com
It will show like.........
No comments:
Post a Comment