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();  
            }  
      
        }  
    
    }
  • 相关阅读:
    推荐一个JavaScript触发器插件,可通过指定频次、指定时间内触发指定的处理函数
    TortoiseGit for windows安装与配置
    Postgresql 迁移随笔一
    三边定位 c#
    unset变量释放内存不起作用
    局域网下 连接别人的数据库授权
    iconv 参数详解
    urlencode()和rawurlencode()区别
    php数组函数
    php://input和parse_str()使用
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6122995.html
Copyright © 2011-2022 走看看