ganymed-ssh2使用
通过maven库获取ganymed-ssh2-262.jar,这是一个实现了ssh2协议的工具包,可以远程连接linux机器,执行命令,有些工作全靠它了
示例代码如下:
<!--首先要建立连接,传入ip(默认端口22),登录用户名和密码-->
private static Connection getConnection(String hostname, String username, String password) throws Exception {
Connection conn = null;
try {
conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed.");
}
} catch (Exception e) {
throw new IOException("username or password error.");
}
return conn;
}
<!--执行一条命令,传入connect相关参数,命令和超时时间-->
public static String execRemoteCommand(String hostname, String username, String password, String command, long timeout)
throws Exception {
Connection conn = getConnection(hostname, username, password);
StringBuilder sb = new StringBuilder();
Session session = null;
try {
session = conn.openSession();
session.requestPTY("vt100", 80, 24, 640, 480, null);
session.execCommand(command);
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
long start = System.currentTimeMillis();
char[] arr = new char[512];
int read;
int i = 0;
while (true) {
read = br.read(arr, 0, arr.length);
if (read < 0 || (System.currentTimeMillis() - start) > timeout * 1000) {
break;
}
sb.append(new String(arr, 0, read));
i++;
}
} finally {
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
}
return sb.toString();
}
<!--执行多条命令,传入connect相关参数,命令和超时时间-->
public static String execRemoteCommand(String hostname, String username, String password, String[] command, long timeout)
throws Exception {
Connection conn = getConnection(hostname, username, password);
StringBuilder sb = new StringBuilder();
Session session = null;
try {
for (int t = 0; t < command.length; t++) {
session = conn.openSession();
session.requestPTY("vt100", 80, 24, 640, 480, null);
session.execCommand(command[t]);
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
long start = System.currentTimeMillis();
char[] arr = new char[512];
int read;
int i = 0;
while (true) {
read = br.read(arr, 0, arr.length);
if (read < 0 || (System.currentTimeMillis() - start) > timeout * 1000) {
break;
}
sb.append(new String(arr, 0, read));
i++;
}
session.close();
}
} finally {
if (conn != null) {
conn.close();
}
}
return sb.toString();
}
最近用这个工具包做了个远程下载的功能
OutputStream out = response.getOutputStream();
Connection conn = getConnection(sshcfg.getHost(), sshcfg.getUsername(), sshcfg.getPassword());
SCPInputStream ins=null;
try {
SCPClient scpClient = conn.createSCPClient();
ins = scpClient.get(fpath);
//InputStream stdout = new StreamGobbler(ins);
byte[] arr = new byte[512];
int read;
while (true) {
read = ins.read(arr);
if (read < 0) break;
out.write(arr);
}
//ins.close(); } finally {
if(ins!=null)ins.close();
if (conn != null) {
conn.close();
}
}
注意:new InputStreamReader(stdout)这个的使用的是默认的字符编码,如果执行”cat **.log“而文件中有中文,应当指定编码,比如:new InputStreamReader(stdout,"GBK"),否则有可能出现乱码
ganymed-ssh2使用的更多相关文章
- Ganymed SSH-2 for Java
Ganymed SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/,最新的更新时间为2006年10月,在用之前,请仔细 ...
- Ganymed实现基本的自动化部署API
Ganymed SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/,最新的更新时间为2006年10月,在用之前,请仔细 ...
- Java ssh 访问windows/Linux
Java ssh 访问windows/Linux 工作中遇到的问题: Java code运行在一台机器上,需要远程到linux的机器同时执行多种命令.原来采用的方法是直接调用ssh命令或者调用pli ...
- java连接远程服务器之manyged-ssh2 (windows和linux)
一.所需要的jar包 需要借助Ganymed SSH的jar包: ganymed-ssh2-262.jar 下载地址: http://www.ganymed.ethz.ch/ssh2/ API详情: ...
- java linux ssh jar
Ganymed SSH-2 for Java http://www.ganymed.ethz.ch/ssh2/ Ganymed SSH-2 for Java is a library which im ...
- java 调用shell命令
原文:http://kongcodecenter.iteye.com/blog/1231177 Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar) ...
- java程序远程连接Linux服务器
JSCH或 Ganymed Ganymed: Ganymed SSH-2 for Java是用纯Java实现SSH-2协议的一个包. 可以利用它直接在Java程序中连接SSH服务器.官网地址为 htt ...
- 使用ganymed工具调用ssh2
需要引入ganymed-ssh2-build210.jar包. 其实很简单.所以直接贴代码,代码说话. package com.eshore.framework.util; import java.i ...
- ch.ethz.ssh2.Session和com.jcraft.jsch.Session
通过Jsch连接step 1引入jar包<!-- jcraft包 --> <dependency> <groupId>com.j ...
- Java SSH远程执行Shell命令、shell脚本实现(Ganymed SSH)
jar包下载地址: http://www.ganymed.ethz.ch/ssh2/ 此源码的好处就是没有依赖很多其他的包,拷贝过来干干净净.具体代码实现可以看下文,或参考官方文档,在下载的压缩包里g ...
随机推荐
- RabbitMQ 入门 Helloworld -摘自网络
本系列教程主要来自于官网入门教程的翻译,然后自己进行了部分的修改与实验,内容仅供参考. “Hello world” of RabbitMQ 1.Windows下RabbitMQ的安装 下载Erlang ...
- Android 不同应用通过SharedPreference实现共享数据
Android不同应用之间数据的共享有许多方式,但是我觉得还是使用sharedPreference比较简单和轻量级.如果程序B想要访问程序A的sharedPreference可以通过下面的语句来实现: ...
- ocp 1Z0-051 71-105题解析
71. Which arithmeticoperations can be performed on a column by using a SQL function that is builtint ...
- javascript判断NaN
功能: isNaN() 函数用于检查其参数是否是非数字值. 语法: isNaN(x) x 必需.要检测的值. 返回值: 如果 x 是特殊的非数字值 NaN(或者能被转换为这样的值),返回的值就是 tr ...
- Serializable 序列化为文件
package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- exit和_exit的区别
参考 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html p.p1 { margin: 0.0px 0.0px 0.0px ...
- AxWindowsMediaPlayer创建、添加播放列表(C#)
// 创见打开对话框对象实例 OpenFileDialog openFileDialog = new OpenFileDialog(); //设置为可以打开多个文件 ...
- Node.js简单介绍
Node.js是一个能够让javascript执行在server上的平台,既是语言又是平台. Node.js是一个实时web应用程序的平台. Node.js有强大的包管理器npm,故node相关软件安 ...
- delphi 16 网页缩放
网页放大 网页缩小 WebBrowser1.OleObject.Document.Body.Style.Zoom := 0.50; 缩放网页 Ctrl+中键↑ 放大 Ctrl+中键↓ ...
- [Jest] Track project code coverage with Jest
Jest comes pre-packaged with the ability to track code coverage for the modules you're testing, but ...