PingTraceUtil.java 3.25 KB
package com.sitech.ismp.util;

import java.util.Date;

import com.adventnet.utils.agent.RunCmd;
/**
 * Created by IntelliJ IDEA.
 * User: LJ
 * Date: 2010-2-23
 * Time: 11:09:02
 * To change this template use File | Settings | File Templates.
 */
public class PingTraceUtil {
    public PingTraceUtil()
  {
  }

  public String getResult(String s, int i)
  {
      String s1;
      if(i == Ping)
          s1 = getPingCmd(s);
      else
      if(i == Trace)
          s1 = getTraceCmd(s);
      else
          throw new IllegalArgumentException("command cannot be equal to " + i + " it must either be 0 or 1");
      return execCmd(s1);
  }

  public String getPingCmd(String s)
  {
      String s1 = System.getProperty("os.name").toLowerCase();
      //System.out.println("#################the system version is "+s1);
      String s3 = "ping -n 3";
      String s4 = "1";
      String s2;
      if(s1.indexOf("sunos") != -1 || s1.indexOf("solaris") != -1)
      {
          s3 = "/usr/sbin/ping -s";
          s2 = new String(s3 + " " + s + " 1 " + s4);
      } else
      if(s1.indexOf("linux") != -1)
      {
          s3 = "/bin/ping -c 1 -w 1 ";
          s2 = new String(s3 + " " + s);
      } else
      if(s1.indexOf("freebsd") != -1)
      {
          s3 = "/sbin/ping -c 1";
          s2 = new String(s3 + " " + s);
      } else
      if(s1.indexOf("windows") != -1)
      {
          s3 = "ping -n 3";
          s2 = new String(s3 + " " + s);
      }
      else
      if(s1.indexOf("hp-ux") != -1)
      {
          s3 = "ping ";
          s2 = new String(s3 + " " + s+" -n 3 -m 3");
      } 
      else
      {
          s2 = new String(s3 + " " + s);
      }
      return s2;
  }

  public String getTraceCmd(String s)
  {
      String s1 = "30";
      String s2 = "tracert";
      String s3 = s2 + " " + s;
      String s4 = System.getProperty("os.name").toLowerCase();
     // System.out.println("#################the system version is "+s4);
      if(s4.indexOf("linux") != -1)
          s3 = "/usr/sbin/traceroute -m " + s1 + " " + s;
      else
      if(s4.indexOf("freebsd") != -1)
      {
          s2 = "/sbin/traceroute -c 1";
          s3 = new String(s2 + " " + s);
      } else
      if(s4.indexOf("windows") != -1)
          s3 = "tracert -h " + s1 + " " + s;
      else      
      if(s4.indexOf("sunos") != -1 || s4.indexOf("solaris") != -1)
          s3 = "/usr/sbin/traceroute -m " + s1 + " " + s;
      else
          s3 = new String(s2 + " " + s);
      return s3;
  }

  private String execCmd(String s)
  {
      int i = 60000;
     // System.out.println("###################execCmd :"+s);
      RunCmd runcmd = new RunCmd(s);
      Date date = new Date();
      runcmd.start();
      while(runcmd.isAlive())
      {
          try
          {
              Thread.sleep(100L);
          }
          catch(Exception exception) { }
          Date date1 = new Date();
          if(date1.getTime() > date.getTime() + (long)i)
          {
              runcmd.stopCommand();
              return "Timed out after " + i / 1000 + " " + "secs" + ": " + s;
          }
      }
      //System.out.println("#######################PingTraceUtil runcmd.stdout.toString():"+runcmd.stdout.toString());
      return runcmd.stdout.toString();
  }

  public static int Ping = 0;
  public static int Trace = 1;
}