需要下载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. Android 自定义ScrollView ListView 体验各种纵向滑动的需求

      分类: [android 进阶之路]2014-08-31 12:59 6190人阅读 评论(10) 收藏 举报 Android自定义ScrollView纵向拖动     转载请标明出处:http: ...

  2. YTU 3007: 顺序串的基本运算

    3007: 顺序串的基本运算 时间限制: 1 Sec  内存限制: 128 MB 提交: 1  解决: 1 题目描述 编写一个程序,实现顺序串的各种基本运算,主函数已给出,请补充每一种方法. 1.建立 ...

  3. jquery 判断checkbox是否为空的三种方法

    //方法一: if ($("#checkbox-id")get(0).checked) { // do something } //方法二:也适用于单选按钮 if($('#chec ...

  4. javascript学习(三) 内置对象

    一:事件(Event)对象 在触发dom事件的时候都会产生一个event对象 type   获取事件类型 target  获取事件目标 stopPropagation()  阻止事件冒泡 preven ...

  5. GUI_Delay函数

    GUI_Delay()函数 使用GUI_Delay()函数时,对于其延时时间不确定,明明设置为最小值1,延时时间 仍旧太长,不能达到需求.遂决定研究明白其实现机理. uC/OS-II使用OSTimeD ...

  6. Mysql-学习笔记(==》函数的建立与使用 十)

    函数的建立与使用 USE db;SELECT sname,sscore,CASE WHEN sscore>=90 THEN '优秀'WHEN sscore>=70 THEN '良好'WHE ...

  7. 2016年12月14日 星期三 --出埃及记 Exodus 21:9

    2016年12月14日 星期三 --出埃及记 Exodus 21:9 If he selects her for his son, he must grant her the rights of a ...

  8. zImage.img、ramdisk.img、system.img、userdata.img介绍及解包、打包方法

    ramdisk.img system.img userdata.img介绍及解包.打包方法 Android 源码编译后,在out/target/product/generic下生成ramdisk.im ...

  9. windows10 IOT +Azure会议概要总结

    windows10 IOT +Azure会议概要总结 会议资料将放到https://channel9.msdn.com/Blogs/WinHEC FAQ:msftsziot@microsoft.com ...

  10. winfrom增删改查

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...