需要下载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();
} } }

ganymedssh2 java执行linux命令的更多相关文章

  1. Java 执行linux命令(转)

    转自 http://blog.csdn.net/a19881029/article/details/8063758 java程序中要执行linux命令主要依赖2个类:Process和Runtime 首 ...

  2. java执行linux命令的工具类

    package com.starfast.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ja ...

  3. java执行linux命令

    package com.gtstar.collector; import java.io.BufferedReader;import java.io.IOException;import java.i ...

  4. java 执行linux命令

    原文地址: http://blog.csdn.net/xh16319/article/details/17302947 package scut.cs.cwh; import java.io.Inpu ...

  5. Java程序执行Linux命令

    Java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...

  6. 利用java实现可远程执行linux命令的小工具

    在linux的脚本中,如果不对机器做其他的处理,不能实现在linux的机器上执行命令.为了解决这个问题,写了个小工具来解决这个问题. 后面的代码是利用java实现的可远程执行linux命令的小工具,代 ...

  7. java执行cmd命令和linux命令

    文章出处http://blog.csdn.net/xh16319/article/details/17302947 一:window下执行cmd指定 一:window下执行cmd指定 程序例子: [j ...

  8. Java程序执行Linux命令(JSP运行其他程序)

    java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...

  9. Java调用Linux命令执行

    调用方式 Java调用linux命令执行的方式有两种,一种是直接调用linux命令,一种是将linux命令写到.sh脚本中,然后调用脚本执行. 详细说明 直接调用:使用java中lang包下面的Run ...

随机推荐

  1. linux主次设备号介绍

    1.主设备号与次设备号的功能 在Linux内核中,主设备号标识设备对应的驱动程序,告诉Linux内核使用哪一个驱动程序为该设备(也就是/dev下的设备文件)服务:而次设备号则用来标识具体且唯一的某个设 ...

  2. HDU 4810 Wall Painting

    Wall Painting Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. CentOS搭建Redis集群

    集群原理-redis-cluster架构图 架构细节: (1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail是通过集群中超过半数的 ...

  4. CentOS 安装ftp

    Linux安装ftp组件 1 安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. [root@bogon ~]# yum -y insta ...

  5. ios布局约束

    http://blog.csdn.net/pucker/article/details/41843511 除了使用Pin菜单之外,也可以直接在文档结构窗口中或者设计器中通过拖拽建立约束.在文档结构窗口 ...

  6. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  7. SqlSever基础 dateadd month 增加五个月

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  8. CodeForces 432B Football Kit

     Football Kit Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  9. FZU 2144 Shooting Game

    Shooting Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  10. about_Execution_Policies

    https://technet.microsoft.com/en-us/library/hh847748.aspx?f=255&MSPPError=-2147217396 在powershel ...