本帖最后由 hcdieche 于 2011-08-19 10:59:31 编辑

解决方案 »

  1.   

    Runtime.getRuntime()方法有能用的例子也可以的
      

  2.   

    兄弟 我写了个给你看看 贴个代码public class Monitor
    {
        private Connection conn;
        private String ipAdd;
        private String charset = Charset.defaultCharset().toString();
        private String userName;
        private String password;
        
        /**
         * 初始化变量
         * 定义构造函数,创建对象时初始化变量
         * @param ipAdd    liunx服务器IP
         * @param charset  对返回结果集编码
         * @param userName 用户名
         * @param password 密码
         */
        public Monitor(String ipAdd,String charset,String userName,String password)
        {
            this.ipAdd = ipAdd;      
            this.userName = userName;
            this.password = password;
            
            if(charset != null)
            {
                this.charset = charset;
            }
        }
        
        /**
         * 远程登录Linux服务器
         * @return 标示是否登录成功
         * @throws IOException   
         */
        public boolean login() throws IOException
        {
            conn = new Connection(ipAdd);
            //连接
            conn.connect();
            //认证登录
    //        return conn.authenticateWithPassword(userName, password);
            File keyFile = new File("D:\\ssh\\id_dsa");
            return conn.authenticateWithPublicKey("root", keyFile, null);
        }
        
        /**
         * 执行Shell脚本
         * 开启一个Session会话,执行sh脚本
         * @param cmds 被执行的脚本
         * @return     结果集文本
         * @throws IOException
         */
        public String exec(String cmds)
        {
            InputStream in = null;
            String result = "";
            
            try
            {
                if(this.login())
                {
                    //打开一个会话
                    Session session = conn.openSession();
                    session.execCommand(cmds);
                    in = session.getStdout();
                    result = this.processStdout(in, charset);              
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                conn.close();
            }
            
            return result;
        }
        
        
        /**
         * 解析流获取字符串信息
         * @param in        输入字节流
         * @param charset   编码
         * @return          字符串信息
         * @throws IOException
         */
        public String processStdout(InputStream in,String charset)
        {
            byte[] byt = new byte[1];
            StringBuffer sb = new StringBuffer();
            
            try
            {
                while(in.read(byt) != -1)
                {
                    sb.append(new String(byt,charset));
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            
            return sb.toString();
        }
        
        /**
         * 测试接口
         * @param args
         */
        public static void main(String[] args)
        {
            Monitor test = new Monitor("10.78.222.111","UTF-8","","");
            System.out.println(test.exec("sh -c /opt/yanpan/helloword.sh"));
        }