ganymed-ssh2-build210.jar 的使用
2015-09-18 14:46
477 查看
ganymed-ssh2简介:
Ganymed SSH-2 for Java是用纯Java实现SSH-2协议的一个包。可以利用它直接在Java程序中连接SSH服务器。Ganymed
SSH-2支持SSH对话(远程命令执行和shell访问),本地和远程端口转发,本地数据流转发,X11转发和SCP。这些都没有依赖任何JCE
provider,而且所有这些都包含加密的功能。
连接远程服务器,新建一个java工具类,将其命名为CommandRunner;
创建一个连接服务器的静态方法:
[java] view
plaincopy
public static Connection getOpenedConnection(String host, String username,
String password) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("connecting to " + host + " with user " + username
+ " and pwd " + password);
}
Connection conn = new Connection(host);
conn.connect(); // make sure the connection is opened
boolean isAuthenticated = conn.authenticateWithPassword(username,
password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
return conn;
}
测试代码:
[java] view
plaincopy
public static void main(String[] args) {
Connection conn = null;
try {
conn = CommandRunner.getOpenedConnection("172.16.18.141", "root",
"123456");
if (null != conn) {
System.out.println("连接服务器成功!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.close();
conn = null;
}
}
至此,连接服务器的静态方法完成,但是这样处理会存在一个问题,就是我们都知道ssh默认端口是22,如果服务器的ssh 端口不是22,那么这个连接服务器的代码是不是就不可以用了啦,所以需要简单的修改下 ,修改如下:
方法增加一个端口参数:
[java] view
plaincopy
public static Connection getOpenedConnection(String host, String username,
String password,int port) throws IOException {
连接的地方将参数放进去:
[java] view
plaincopy
Connection conn = new Connection(host,port);
这样不论ssh端口改为什么,我们底层的这个连接方法都不在需要改动了。
分类: J***A shell SSH22014-02-24
18:42 846人阅读 评论(1) 收藏 举报
利用Ganymed SSH-2 for Java 连接到远程服务器,然后执行shell命令;
首先我们再在之前CommandRunner类中再添加一个执行shell命令的方法,具体如下所示:
[java] view
plaincopy
public static String execShellScript(String host, String username,
String password,
String cmd, int port) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("running SSH cmd [" + cmd + "]");
}
Connection conn = null;
Session sess = null;
InputStream stdout = null;
BufferedReader br = null;
StringBuffer buffer = new StringBuffer("exec result:");
buffer.append(System.getProperty("line.separator"));// 换行
try {
conn = getOpenedConnection(host, username, password, port);
sess = conn.openSession();
sess.execCommand(cmd);
stdout = new StreamGobbler(sess.getStdout());
br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
// attention: do not comment this block, or you will hit
// NullPointerException
// when you are trying to read exit status
String line = br.readLine();
if (line == null)
break;
buffer.append(line);
buffer.append(System.getProperty("line.separator"));// 换行
if (logger.isInfoEnabled()) {
logger.info(line);
}
}
} finally {
sess.close();
conn.close();
}
return buffer.toString();
}
测试代码:
[java] view
plaincopy
public static void main(String[] args) {
String cmd = "uname -a";
try {
String info = CommandRunner.execShellScript("172.16.18.141", "root",
"123456",cmd,22);
System.out.println("info is:"+info);
} catch (IOException e) {
e.printStackTrace();
}
}
执行结果
log4j:WARN No appenders could be found for logger (com.ssh2.shell.ganymed.CommandRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
info is:exec result:
Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
转自:http://blog.csdn.net/wangmuming/article/details/19835631
Ganymed SSH-2 for Java是用纯Java实现SSH-2协议的一个包。可以利用它直接在Java程序中连接SSH服务器。Ganymed
SSH-2支持SSH对话(远程命令执行和shell访问),本地和远程端口转发,本地数据流转发,X11转发和SCP。这些都没有依赖任何JCE
provider,而且所有这些都包含加密的功能。
Ganymed SSH-2 for Java系列2之连接远程服务器
连接远程服务器,新建一个java工具类,将其命名为CommandRunner;创建一个连接服务器的静态方法:
[java] view
plaincopy
public static Connection getOpenedConnection(String host, String username,
String password) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("connecting to " + host + " with user " + username
+ " and pwd " + password);
}
Connection conn = new Connection(host);
conn.connect(); // make sure the connection is opened
boolean isAuthenticated = conn.authenticateWithPassword(username,
password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
return conn;
}
测试代码:
[java] view
plaincopy
public static void main(String[] args) {
Connection conn = null;
try {
conn = CommandRunner.getOpenedConnection("172.16.18.141", "root",
"123456");
if (null != conn) {
System.out.println("连接服务器成功!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.close();
conn = null;
}
}
至此,连接服务器的静态方法完成,但是这样处理会存在一个问题,就是我们都知道ssh默认端口是22,如果服务器的ssh 端口不是22,那么这个连接服务器的代码是不是就不可以用了啦,所以需要简单的修改下 ,修改如下:
方法增加一个端口参数:
[java] view
plaincopy
public static Connection getOpenedConnection(String host, String username,
String password,int port) throws IOException {
连接的地方将参数放进去:
[java] view
plaincopy
Connection conn = new Connection(host,port);
这样不论ssh端口改为什么,我们底层的这个连接方法都不在需要改动了。
Ganymed SSH-2 for Java系列3之执行远程shell 命令
分类: J***A shell SSH22014-02-2418:42 846人阅读 评论(1) 收藏 举报
利用Ganymed SSH-2 for Java 连接到远程服务器,然后执行shell命令;
首先我们再在之前CommandRunner类中再添加一个执行shell命令的方法,具体如下所示:
[java] view
plaincopy
public static String execShellScript(String host, String username,
String password,
String cmd, int port) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("running SSH cmd [" + cmd + "]");
}
Connection conn = null;
Session sess = null;
InputStream stdout = null;
BufferedReader br = null;
StringBuffer buffer = new StringBuffer("exec result:");
buffer.append(System.getProperty("line.separator"));// 换行
try {
conn = getOpenedConnection(host, username, password, port);
sess = conn.openSession();
sess.execCommand(cmd);
stdout = new StreamGobbler(sess.getStdout());
br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
// attention: do not comment this block, or you will hit
// NullPointerException
// when you are trying to read exit status
String line = br.readLine();
if (line == null)
break;
buffer.append(line);
buffer.append(System.getProperty("line.separator"));// 换行
if (logger.isInfoEnabled()) {
logger.info(line);
}
}
} finally {
sess.close();
conn.close();
}
return buffer.toString();
}
测试代码:
[java] view
plaincopy
public static void main(String[] args) {
String cmd = "uname -a";
try {
String info = CommandRunner.execShellScript("172.16.18.141", "root",
"123456",cmd,22);
System.out.println("info is:"+info);
} catch (IOException e) {
e.printStackTrace();
}
}
执行结果
log4j:WARN No appenders could be found for logger (com.ssh2.shell.ganymed.CommandRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
info is:exec result:
Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
转自:http://blog.csdn.net/wangmuming/article/details/19835631
相关文章推荐
- java execute、executeQuery和executeUpdate之间的区别
- MyGUI中的内存泄漏
- RLEH7配置samba,提示WARNING: Ignoring invalid value 'share' for parameter 'security'
- 改变UITextField的Placeholder颜色
- 加载SpriteBuilder中的scene为何不能带后缀
- 加载SpriteBuilder中的scene为何不能带后缀
- 加载SpriteBuilder中的scene为何不能带后缀
- UITableview控件基本使用
- mosquitto-1.4.2安装配置教程
- JSONValue.parse出错
- win7 64位系统下运行Jbuilder2006
- iOS学习笔记—— UINavigationController(一)实现页面跳转&添加左右侧按钮
- this compilation unit is not on the build path of a java project
- android ui分析 - 今日头条UI主界面
- 猫学习IOS(十五)UI以前的热的打砖块游戏
- HDU5400 Arithmetic Sequence
- Implement Stack using Queues
- xcode7.0-iOS9新特性之三:界面适配利器:UIStackView
- UITabBarItem 如何触发点击事件
- 【Mockplus教程】重命名