2010-04-13 9 views
6

Java'nın ICMP'ler ve traceroute için temel değerleri yoktur. Bu nasıl aşılır? Temel olarak * nix ve Windows'da çalışması gereken kodları yapıyorum ve her iki platformda da çalışacak bir kod parçasına ihtiyacım var.Java'da ICMP'ler ve traceroutes nasıl yapılır

cevap

4

Java'da izleme yolu komutunu "uygulamak" için bugün yazdığım şey. Sadece Windows'da test ettim, fakat Linux için de çalışmalı, ancak Linux için birçok traceroute aracı mevcut olsa da, muhtemelen bu programların varlığı için bazı kontroller olmalı.

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; 
} 
+2

Durum os.toLowerCase() öğesinde değiştirilmelidir. ("Win") – Hassan

+0

** convertStreamToString ** yöntemini nerede kullandı? – Omore

+0

@Omore Nerede tanımlandığını soruyor musunuz? Bence bu yazdığım özel bir yöntem. Artık kodlara erişimim yok, böylece nasıl uygulanacağını anlamanız gerekecek. Eğer yaparsanız, lütfen cevabımı içerecek şekilde güncelleyin; Bunu yapmak için puan alırsınız. –

İlgili konular