需要下载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. SlickGrid example 2: 按想要的风格展示列

    先上效果图. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...

  2. 测试post

    http://www.atool.org/httptest.php 这里也可以测试 火狐有插件:HttpRequester Chrome有插件:Postman

  3. 什么是RAW数据?

    现在数码照片的存储格式,以及在网络上应用的最普遍的图片格式就是JPEG格式了(Joint Photographic Expert Group).但是作为一种有损的图像压缩格式(现在也有少数数码相机提供 ...

  4. python-dev无法安装

    安装tensorflow之前需要先安装python-dev, 这个不能用pip install来安装,只能用sudo apt-get install来安装.安装后会发现还要先装另外两个东西,但是装这两 ...

  5. noip2011提高组day1+day2解题报告

    Day1 T1铺地毯https://www.luogu.org/problem/show?pid=1003 [题目分析] 全部读入以后从最后一个往前找,找到一个矩形的范围覆盖了这个点,那这个矩形就是最 ...

  6. 基于kickstart定制自动化安装的linux镜像系统

    使用软件和平台 1.基于平台:                  Vmware workstation 8.0 2.基于系统镜像:               rhel-server-5.8-i386 ...

  7. 正则表达式的使用(C++)

    1.判断手机号是否合法 const regex phonepattern("^[1]+[3,5]+\\d{9}$"); const regex Mobilepatten(" ...

  8. 一、java环境搭建

    结论: 1.jdk包括jre,jre包括jvm. 2.eclipse ide依赖环境变量.如果未设置,在启动eclipse工具会提示:返回码是13.

  9. 【leetcode❤python】Convert a Number to Hexadecimal

    #-*- coding: UTF-8 -*- class Solution(object):    hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6', ...

  10. pch和info.plist初探

    整理下之前的关于pch和info.plist文件,一下文件部分内容是从传智博客的课件里截取