本人想实现如下一个功能:服务器开机后,自动检测本网段中的各主机是否处于开机状态。
初步一个想法是,服务器调用PING命令,循环PING本网段内的各主机,PING通则该机处于开机状
问题:1、不知如何在JAVA语言中调用PING命令
      2、如在网段主机中有防火墙,PING不通的情况下如何获知主机是否开机非常急切的盼望各位DX帮忙,非常非常感谢!

解决方案 »

  1.   

    刚才查了一下,如此问题好像应该是调用Runtime.getRuntime().exec();
    但如何判别结果呢?难道是用字符串匹配的方法吗?
    还要请高人解,非常感谢
      

  2.   

    Process prs = Runtime.getRuntime().exec("cmd /c ping ip");
    BufferedInputStream   bis   =   new   BufferedInputStream(prs.getInputStream());  
    InputStreamReader   reader   =   new   InputStreamReader(bis,   "UTF-8");
    然后自己分析
      

  3.   

    好的,非常感谢xredleaf(萧红叶) 
    那第二个问题呢?有没有什么好的解决方式呢?
    再次感谢!
      

  4.   

    /*
     * Ping.java
     *
     * Created on 2007年7月3日, 下午2:44
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */package pingtest;import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;/**
     *
     * @author tommy
     */
    public class Ping 
    {    
        public Ping()
        {
            for(int i =0;i<255;i++)
                result[i] = 0;
        }
        
        public void scan()
        {
            for(int i=70;i<85;i++)
            {
                String tmp = exec + i;            try 
                {                
                    Process pr = Runtime.getRuntime().exec(tmp);
                    BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                    
                    while   (true)   
                    {
                        String s = br.readLine();
                        if(s==null)
                            break;
                        if (s.contains("TTL"))
                            result[i] = 1;           
                        System.out.println(s);
                    }
                    br.close();
                } 
                catch (IOException ex) 
                {
                    ex.printStackTrace();
                }
            }
        }
        
        public   static   void     main(String   args[])
        {
            Ping a = new Ping();
            a.scan();
            for(int i =0;i<255;i++)
                System.out.println("result["+i+"] = "+a.result[i]);
        }
        
        private static String exec = new String("cmd /c ping 100.3.3.");
        public int[] result = new int[255];
    }