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();
} } }
ganymedssh2 java执行linux命令的更多相关文章
- Java 执行linux命令(转)
转自 http://blog.csdn.net/a19881029/article/details/8063758 java程序中要执行linux命令主要依赖2个类:Process和Runtime 首 ...
- java执行linux命令的工具类
package com.starfast.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ja ...
- java执行linux命令
package com.gtstar.collector; import java.io.BufferedReader;import java.io.IOException;import java.i ...
- java 执行linux命令
原文地址: http://blog.csdn.net/xh16319/article/details/17302947 package scut.cs.cwh; import java.io.Inpu ...
- Java程序执行Linux命令
Java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...
- 利用java实现可远程执行linux命令的小工具
在linux的脚本中,如果不对机器做其他的处理,不能实现在linux的机器上执行命令.为了解决这个问题,写了个小工具来解决这个问题. 后面的代码是利用java实现的可远程执行linux命令的小工具,代 ...
- java执行cmd命令和linux命令
文章出处http://blog.csdn.net/xh16319/article/details/17302947 一:window下执行cmd指定 一:window下执行cmd指定 程序例子: [j ...
- Java程序执行Linux命令(JSP运行其他程序)
java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...
- Java调用Linux命令执行
调用方式 Java调用linux命令执行的方式有两种,一种是直接调用linux命令,一种是将linux命令写到.sh脚本中,然后调用脚本执行. 详细说明 直接调用:使用java中lang包下面的Run ...
随机推荐
- c#游戏 剪刀石头
电脑using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace sh ...
- Shuffle'm Up 分类: 函数 POJ 查找 2015-08-09 17:01 6人阅读 评论(0) 收藏
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7529 Accepted: 3466 Descript ...
- 关于Animation动画事件的几项测试
测试版本unity5.3.4 1.当帧率不足时,Animation会跳帧,假如跳帧超过动画事件的帧,动画事件也会执行. 2.当Animation混合时,混合中的动画不管哪个当前有动画事件,都会执行. ...
- laravel框架总结(一) -- 请求和响应
一.laravel请求 1.获取请求 1>获取请求的 URI path 方法会返回请求的 URI.所以,如果接收到的请求目标是 http://domain.com/foo/bar,那么 path ...
- range函数
th> a=torch.range(,) [.0001s] th> a [torch.DoubleTensor of size ] [.0002s] th> a=torch.rang ...
- hdu Interesting Fibonacci
Interesting Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A
Description You are given names of two days of the week. Please, determine whether it is possible th ...
- Objective-C协议与非正式协议
http://blog.csdn.net/siemenliu/article/details/7836499
- JavaScript(1)
JavaScript(1) 第一次见到JavaScript的时候,看到了熟悉的"Java",原来都是骗人的,李鬼不是李逵呀=.= 然而发现这个东西还是特别实用的,和Java比起来它 ...
- SqlSever基础 where 与 group by组合起来 处理数据
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...