pom文件:

<dependency>
<groupId>org.jvnet.hudson</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210-hudson-1</version>
</dependency>

Java代码:

package com.gosun.utils;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler; import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset; /**
*
* storm 远程调用脚本..
* @author chenwen
*
*/
public class ShellRPC { private static Connection conn;
/** 远程机器IP */
private static String ip;
/** 用户名 */
private static String userName;
/** 密码 */
private static String password;
private static String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; /**
* 登录
* @return
* @throws IOException
*/
private static boolean getConnection(String username, String password) throws IOException {
ip = ip;
userName = userName;
password = password; conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(userName, password);
} /**
* 执行脚本
*
* @param cmds
* @return
* @throws Exception
*/
public static int exec(String cmds) throws Exception {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
int ret = -1;
try {
if (getConnection()) {
// Open a new {@link Session} on this connection
Session session = conn.openSession();
// Execute a command on the remote machine.
session.execCommand(cmds); stdOut = new StreamGobbler(session.getStdout());
outStr = processStream(stdOut, charset);
System.out.println("outStr" + outStr);
stdErr = new StreamGobbler(session.getStderr());
outErr = processStream(stdErr, charset);
System.out.println("outErr" + outErr);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); ret = session.getExitStatus();
} else {
throw new Exception("登录远程机器失败" + ip); // 自定义异常类 实现略
}
} finally {
if (conn != null) {
conn.close();
}
stdOut.close();
stdErr.close();
}
return ret;
} /**
* @param in
* @param charset
* @return
* @throws Exception
*/
private static String processStream(InputStream in, String charset) throws Exception {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
while (in.read(buf) != -1) {
sb.append(new String(buf, charset));
}
return sb.toString();
} }

Java 执行远程主机shell命令代码的更多相关文章

  1. java执行linux shell命令,并拿到返回值

    package com.pasier.xxx.util; import java.io.IOException; import java.io.InputStream; import java.nio ...

  2. Android 执行 adb shell 命令

    Android 执行Adb shell 命令大多需要root权限,Android自带的Runtime. getRuntime().exec()容易出错,在网上找到了一个执行adb shell命令的类 ...

  3. recovery 升级过程执行自定义shell命令

    有时候我们需要,在升级的过程中,执行一些shell命令,来完成我们的一些需求,利用升级过程,进行一些特殊化的操作,思路如下: 第一: 把我们需要执行的命令,写成一个test.sh脚本,然后在recov ...

  4. ssh伪登陆执行远程主机脚本命令 C程序基于ssh passwordless执行远程主机命令及基于配置文件的验证伪登陆执行命令

    1,基于有密码及免秘钥在远程主机上执行命令及脚本 ssh -T ip "CLI or shell.sh"; 2,C程序实现上述功能--基于password-less

  5. 在Jenkins中配置执行远程shell命令

    1.想要 远程登录到linux服务器并执行相应的shell脚本,需要在jenkins上安装插件enkins SSH plugin 2. 安装了这个插件后,进入系统的配置管理中配置 SSH remote ...

  6. shell脚本--php执行普通shell命令

    这里只演示一些普通的shell命令,一些需要root用户权限执行的命令,请参考:php以root权限执行shell命令 php执行shell命令,可以使用下面几个函数: string system ( ...

  7. 【Devops】【docker】【CI/CD】关于jenkins构建成功后一步,执行的shell命令详解+jenkins容器运行宿主机shell命令的实现方法

    1.展示这段shell命令 +详解 #================================================================================= ...

  8. 在docker中执行linux shell命令

    在docker中执行shell命令,需要在命令前增加sh -c,例如: docker run ubuntu sh -c 'cat /data/a.txt > b.txt' 否则,指令无法被正常解 ...

  9. [Python2.x] 利用commands模块执行Linux shell命令

    用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...

随机推荐

  1. Android项目实战(四十九):Andoird 7.0+相机适配

    解决方案类似: Android项目实战(四十):Andoird 7.0+ 安装APK适配 解决方法: 一.在AndroidManifest.xml 文件中添加 四大组件之一的 <provider ...

  2. CentOS 7.x默认没有ifconfig?!

    刚装了CentOS 7.0,安装界面非常漂亮,装完后发现没有ifconfig命令.yum install net-tools后出现. 有两个可能,一个是mini版本的原因,二一个可能我在安装过程中配置 ...

  3. Nginx与Nginx-rtmp-module搭建RTMP视频直播和点播服务器

    一.开发环境 Nginx地址:  http://nginx.org/download/nginx-1.14.2.tar.gz Nginx-rtmp-module地址: https://github.c ...

  4. A problem has been detected and windows has been shut down to prevent damage to your computer.他么啥意思?看这里!【蓝屏】

    A problem has been detected and windows has been shut down to prevent damage to your computer.  检测到问 ...

  5. [20181225]12CR2 SQL Plan Directives.txt

    [20181225]12CR2 SQL Plan Directives.txt --//12C引入SQL PLAN Directives.12cR1版本会造成大量的动态取样,影响性能.许多人把OPTI ...

  6. 几种方法来实现scp拷贝时无需输入密码

    欢迎转载!转载时请注明出处:http://blog.csdn.net/nfer_zhuang/article/details/42646849 前言 我在工作中经常要将一些文件传输到另外一个服务器上, ...

  7. .svn文件夹特别大

    一个项目通过svn管理,迭代开发一年之后,.svn目录达到20G或更大,对于SSD硬盘来说是非常占用空间的,经过我的尝试,可以使用tortoiseSVN自带的cleanup为文件夹瘦身. 操作方法: ...

  8. 排序算法之折半插入排序的思想以及Java实现

    1 基本思想 折半插入排序(binary insertion sort)的基本原理与直接插入排序相同,不同之处在于,确定当前记录在前面有序子数组中的位置时,直接插入排序是采用顺序查找的方法,而折半插入 ...

  9. PHP下载网页

    <?php /*   author:whq   作用:获取网页的内容 */   include "../Snoopy/Snoopy.class.php";class Cute ...

  10. Windows 命令行

     1.d: 进入d盘  2.dir(directory)   显示当前文件夹下的所有内容  3.md (make directory) +文件夹名 在当前文件夹下创建文件夹  4.rd (remove ...