Java에는 ICMP 및 traceroute의 프리미티브가 없습니다. 이것을 극복하는 방법? 기본적으로 필자는 * nix 및 Windows에서 실행되어야하는 코드를 작성 중이며 두 플랫폼 모두에서 실행될 코드 조각이 필요합니다.Java의 ICMP 및 traceroutes 방법
6
A
답변
4
다음은 Java에서 추적 경로 명령을 "구현"하기 위해 작성한 내용입니다. 필자는 Windows에서만 테스트를 해봤지만 Linux에서도 사용할 수있는 몇 가지 traceroute 도구가 있지만 Linux에서도 작동 할 것입니다. 따라서 이러한 프로그램의 존재 여부를 확인해야 할 가능성이 가장 큽니다.
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());
if(errors != "") LOGGER.error(errors);
}
catch (IOException e) {
LOGGER.error("error while performing trace route command", e);
}
return route;
}
1
당신이해야하는 jpcap library (아마 SourceForge jpcap 너무 노력)하고 원하는 기능을 구현하기 위해 ICMPPacket 클래스를 사용합니다.
여기에 jpcap library을 사용하는 Java traceroute implementation입니다.
조건을 os.toLowerCase()로 변경해야합니다. contains ("win") – Hassan
여기서 ** convertStreamToString ** 메소드를 사용하셨습니까? – Omore
@Omore 당신이 어디에 정의되어 있는지 묻고 있습니까? 나는 그것이 내가 작성한 맞춤 방법이라고 생각한다. 코드에 더 이상 액세스 할 수 없으므로 구현 방법을 알아 내야합니다. 그럴 경우 포함 시키려면 내 대답을 업데이트하십시오. 당신은 그것을하기위한 포인트를 얻을 것이다. –