zoukankan      html  css  js  c++  java
  • ganymedssh2 java执行linux命令

    需要下载ganymed-ssh2-262.jar

    package ganymed;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import ch.ethz.ssh2.Connection;
    import ch.ethz.ssh2.Session;
    import ch.ethz.ssh2.StreamGobbler;
    
    public class SShUtil {
        public static String execShellScript(String host, String username,  
                String password,  
      
                String cmd, int port) throws IOException {  
      
            System.out.println(("running SSH cmd [" + cmd + "]"));
            Connection connection = null;  
      
            Session sess = null;  
      
            InputStream stdout = null;  
      
            BufferedReader br = null;  
      
            StringBuffer buffer = new StringBuffer("exec result:");  
            buffer.append(System.getProperty("line.separator"));// 换行  
            try {  
      
                
                 connection = new Connection(host, port);
                 connection.connect();
          
                 if (!connection.authenticateWithPassword(username, password)) {
                     throw new RuntimeException("authenticateWithPassword failed");
                 }
                 
                sess = connection.openSession();  
      
                sess.execCommand(cmd);  
      
                stdout = new StreamGobbler(sess.getStdout());  
      
                br = new BufferedReader(new InputStreamReader(stdout));  
      
                while (true) {  
                    String line = br.readLine();  
                    if (line == null)  {
                        break;  
                    }
                    buffer.append(line);  
                    buffer.append(System.getProperty("line.separator"));// 换行  
                    System.out.println(line);
                }  
      
            } finally {
                if(br != null){
                    br.close();
                }
                if(sess != null){
                    sess.close();  
                }
                if(connection != null){
                    connection.close();  
                }
            }  
      
            return buffer.toString();  
      
        }  
        
        public static void main(String[] args) {  
            String cmd = "cd /usr/local/mysql/bin&&./ndb_mgm -e show";  
            try {  
                String info = execShellScript("192.168.1.240", "root", "test",cmd,22);  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
      
        }  
    
    }
  • 相关阅读:
    jQuery--隐藏事件
    正则表达式(全)
    1、简单的页面登录、下拉菜单、数据库查询
    1/多态的应用...
    PHP中的魔术方法:__construct, __destruct , __call,__get, __set, __isset, __unset , __toString, __set,__clone and __autoload
    1、php----自动加载类 __autoload()函数
    1、面向对象上课笔记。。。
    1、遍历数组知识
    1、php基本语法--函数
    1、刚学php感觉真有意思!
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6122995.html
Copyright © 2011-2022 走看看