实际应用中,有时候需要从web管理界面上,远程去启动其它linux主机上的程序,利用ssh协议可以方便的满足这一需求。事实上hadoop架构中,从nn上启动dn时,就是利用了免密码ssh登录。ganymed-ssh2是一个实现了ssh协议的开源项目,项目地址为:http://ganymed-ssh-2.googlecode.com/ (下载源码要翻强,众所周知的原因),如果只是使用的话,pom.xml添加以下依赖项就行了:

         <dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>

为了方便起见,封装了一个工具类SSHUtil.java(已托管在taobao.org上)

package com.cnblogs.yjmyzz.utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; /**
* SSH工具类(可远程执行其它Linux机器上的Shell命令)
* Created by jimmy on 2015/7/6.
* http://code.taobao.org/p/y-lib/src/trunk/src/main/java/com/cnblogs/yjmyzz/utils/SSHUtil.java
*/
public class SSHUtil { /**
* 连接到主机
*
* @param hostname
* @param username
* @param password
* @return
* @throws Exception
*/
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;
} /**
* 执行远程命令
*
* @param hostname 远程主机IP
* @param username 用户名
* @param password 密码
* @param command 需要执行的命令
* @param timeout 超时时间(秒)
* @return
* @throws Exception
*/
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();
} /**
* 执行远程命令(默认5秒超时)
*
* @param hostname 远程主机IP
* @param username 用户名
* @param password 密码
* @param command 需要执行的命令
* @return
* @throws Exception
*/
public static String execRemoteCommand(String hostname, String username, String password, String command)
throws Exception {
return execRemoteCommand(hostname, username, password, command, 5);
} /**
* 批量执行远程命令
*
* @param hostname 远程主机IP
* @param username 用户名
* @param password 密码
* @param command 需要执行的命令列表
* @param timeout 超时时间(秒)
* @return
* @throws Exception
*/
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();
} /**
* 批量执行远程命令(默认5秒超时)
*
* @param hostname 远程主机IP
* @param username 用户名
* @param password 密码
* @param command 需要执行的命令列表
* @return
* @throws Exception
*/
public static String execRemoteCommand(String hostname, String username, String password, String[] command)
throws Exception {
return execRemoteCommand(hostname, username, password, command, 5);
}
}

使用要点:

1. 如果要连续执行多个命令,用&&连接,比如:先 cd / 切换到根目录,然后再ls 根目录下的所有文件,可以这样调用:

    public static void main(String[] args) {
String hostname = "172.21.129.**";
String username = "root";
String password = "***";
try {
System.out.println(SSHUtil.execRemoteCommand(hostname, username, password,
"pwd&&cd /&&pwd&&ls"));
} catch (Exception e) {
e.printStackTrace();
}
}

上面的命令相当于在同一个session下,连续执行

pwd

cd /

pwd

ls

2. 如果要以后台进程调用命令,传入命令时,直接加 nohup 即可 

  

利用ganymed-ssh2远程执行其它Linux机器上的shell命令的更多相关文章

  1. 在Linux机器上安装telnet命令

    一.查看本机是否安装       telnet #rpm -qa | grep telnet     如果什么都不显示,说明没有安装telnet 二.开始安装 yum install xinetd y ...

  2. 如何将项目部署到远程的Linux机器上的tomcat上

    下面来练习一下如何将本地的一个项目部署到远程的Linux机器上去. 第一步:将要部署到Linux机器上的项目打成一个war包 war包有一个好处tomcat可以自动解压 第二步:将打好的war上传到L ...

  3. 在Linux机器上安装MySQL

    在Linux机器上安装MySQL,仔细认真些就没有问题. CentOS 7下MySQL 5.7安装.配置与应用_数据库技术_Linux公社-Linux系统门户网站 搞不定的话,直接删掉这个MySQL, ...

  4. linux机器上部署多台Tomcat

    在Linux机器上部署多台Tomcat, 我部署的是Tomcat8,只需要一步,即避免端口号冲突. 在解压后的tomcat目录下,修改conf下server.xml. 修改shutdown端口: &l ...

  5. Linux不管上一条命令成功还是失败都执行下一个命令的方法

    转载请注明来源https://www.cnblogs.com/sogeisetsu/p/11407830.html Linux不管上一条命令成功还是失败都执行下一个命令的方法 Linux不管上一条命令 ...

  6. linux服务器上没有jar命令

    在linux服务器上用jar命令解压jar包时,提示找不到jar命令. 但是用java -version查看jdk版本,又可以显示出jdk版本. echo $JAVA_HOME查看环境变量路径,找不到 ...

  7. java调用机器上的shell脚本

    java调用机器上的shell脚本,可以这样方便的通过shell脚本调用本机的C.C++等程序 Process process = null; Runtime runTime = Runtime.ge ...

  8. linux系统上传下载命令rz和sz的教程

    (一)安装方法汇总(注意:一下命令如果没有权限的需要在每个命令前面加一个sudo) 1.安装方法(推荐) sudo yum install lrzsz 2.在安装Linux系统时选中“DialupNe ...

  9. linux利用ssh远程执行多台机器执行同样的命令

    这篇文章主要介绍了ssh远程执行命令方法和Shell脚本实例,本文讲解了ssh执行远程操作方法和远程执行命令shell脚本示例,需要的朋友可以参考下 ssh执行远程操作命令格式代码如下: ssh -t ...

随机推荐

  1. symfony2 路由工作原理及配置

    1.路由是程序的方法和URL的一一映射.

  2. DOM 节点操作

    一.获取节点 方法名 只能document调用 返回单一的值 返回动态集合 getElementById √ √ getElementsByTagName √ getElementsByClassNa ...

  3. 关于rails里集成测试assert_template的写法

    assert_template后面只能跟随模板文件名,不能跟随命名路径.比如routes.rb: get 'login' => 'sessions#new' 在集成测试用例里,只能写成asser ...

  4. css3:盒模型以及box-sizing属性

    文档中的每个元素被描绘为矩形盒子.渲染引擎的目的就是判定大小,属性——比如它的颜色.背景.边框方面——及这些盒子的位置.在CSS中,这些矩形盒子用标准盒模型来描述.这个模型描述了一个元素所占用的空间. ...

  5. Ubuntu 14.04 软件源服务器列表

    http://wiki.ubuntu.com.cn/Template:14.04source 服务器列表 可将 http://cn.archive.ubuntu.com/ubuntu/ 替换为下列任意 ...

  6. web项目修改名称问题

    第一步:鼠标点击项目按F2 ,然后修改名称 第二步:备份web.xml 第三步:鼠标点击项目右键  选properties(一般位于最后面)  再在弹出框中输入WEB 第四步:将备份的web.xml文 ...

  7. 一些性能查询的SQL 备忘

    --检查数据库的等待事件 from v$session_waitwhere event not like 'SQL%' and event not like 'rdbms%' --找出系统中耗时的操作 ...

  8. Parameter 'id' not found. Available parameters are [1, 0, param1, param2]异常

    出现此类异常可能的原因:Mapper.xml文件中的parameterType的类型与传入的参数类型不匹配

  9. innoDB源码分析--缓冲池

    最开始学Oracle的时候,有个概念叫SGA和PGA,是非常重要的概念,其实就是内存中的缓冲池.InnoDB的设计类似于Oracle,也会在内存中开辟一片缓冲池.众所周知,CPU的速度和磁盘的IO速度 ...

  10. 数据分页处理系列之一:Oracle表数据分页检索SQL

      关于Oracle数据分页检索SQL语法,网络上比比皆是,花样繁多,本篇也是笔者本人在网络上搜寻的比较有代表性的语法,绝非本人原创,贴在这里,纯粹是为了让"数据分页专题系列"看起 ...