1 依赖

<!--远程调用shell-->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>

2 shell-java工具类

package com.xinyi.spark.analysis.utils;/**
* @Author: liang.he
* @Desc:
* @Date: Created in 10:37 2018/5/11
*/

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
* @Author: liang.he
* @Desc:
* @Date: Created in 10:37 2018/5/11
*/
public class RemoteShellTool {
private Connection conn;
private String ip;
private String charset = Charset.defaultCharset().toString();
private String userName;
private String password;

public RemoteShellTool(String ip, String userName, String password, String charset) {
this.ip = ip;
this.charset = charset;
this.userName = userName;
this.password = password;
}

public boolean login() throws IOException {
conn = new Connection(ip);
conn.getPort();
conn.connect();
return conn.authenticateWithPassword(userName,password);
}

public String exec (String cmds){
InputStream in = null;
String result = "";
try {
if(!this.login()){
System.out.println("登陆失败!");
return "error";
}
Session session = conn.openSession();
session.execCommand(cmds);

in = session.getStdout();
result = this.processSedout(in,this.charset);
session.close();
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

public String processSedout(InputStream in,String charset){
byte[] buf = new byte[1024];
StringBuffer sb = new StringBuffer();
try {
while (in.read(buf)!=-1){
sb.append(new String(buf,charset));
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}

3 测试调用测试

package spark_recordinfo.spark_recordinfo;/**
* @Author: liang.he
* @Desc:
* @Date: Created in 10:33 2018/5/11
*/

import com.xinyi.spark.analysis.utils.RemoteShellTool;

/**
* @Author: liang.he
* @Desc:
* @Date: Created in 10:33 2018/5/11
*/
public class ShellTest {
public static void main(String[] args) {

RemoteShellTool shellTool = new RemoteShellTool("ip","root","pswd","utf-8");
String result1 = shellTool.exec("/opt/test/test.sh ");
System.out.println(result1);
}
}

转载于:https://www.cnblogs.com/yzlsthl/p/9099306.html

java 调用启动远程shell脚本,启动spark相关推荐

  1. java 调用casperjs_Java程序去调用并执行shell脚本及问题总结(推荐)

    摘要: 该文章来自阿里巴巴技术协会(ATA)精选集 背景 我们在开发过程中,大部分是java开发, 而在文本处理过程中,主要就是脚本进行开发. java开发的特点就是我们可以很早地进行TDDL, ME ...

  2. java 读取ssh2协议得到的数据_Java通过SSH2协议执行远程Shell脚本

    Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar) ganymed-ssh2简介: Ganymed SSH-2 for Java是用纯Java实现SS ...

  3. shell 脚本启动 SpringBoot程序,用 curl 传参给 post、delete、update等请求的body

    shell脚本启动SpringBoot #!/bin/sh ## java 此处是指定jdk启动 export JAVA_HOME=/usr/local/java/jdk1.8.0_181 # jre ...

  4. shell脚本启动停止程序

    目录 shell脚本启动停止重启单个程序 shell脚本启动停止多个程序 编写shell脚本执行springboot项目 jar包 编写shell脚本执行python脚本以及其他程序 shell脚本启 ...

  5. Jenkins执行shell脚本启动tomcat失败解决方法

    Jenkins执行shell脚本启动tomcat失败解决方法 参考文章: (1)Jenkins执行shell脚本启动tomcat失败解决方法 (2)https://www.cnblogs.com/wa ...

  6. linux下shell脚本启动其他可执行程序

    linux下shell脚本启动其他可执行程序 零.前言 一.C++代码 二.shell脚本 三.shell运行效果 零.前言 linux下的项目中经常需要使用shell脚本去启动其他程序的操作,下面是 ...

  7. 解决Linux下无法利用shell脚本启动conda虚拟环境问题

    解决Linux下无法利用shell脚本启动conda虚拟环境问题 问题 直接在脚本中激活conda环境时遇到了一个问题 解决 查看.bashrc文件 .bashrc文件在 /home/用户名 里面,因 ...

  8. linux exec 脚本之家,详解Shell脚本中调用另一个Shell脚本的三种方式

    主要以下有几种方式: Command Explanation fork 新开一个子 Shell 执行,子 Shell 可以从父 Shell 继承环境变量,但是子 Shell 中的环境变量不会带回给父 ...

  9. shell脚本内调用另外一个shell脚本的几种方法

    有时会在一个shell脚本(如test_call_other_shell.sh)中调用另外一个shell脚本(如parameter_usage.sh),这里总结几种可行的方法,这些方法在linux上和 ...

最新文章

  1. 记一个开发中遇到react-native flatList 的坑
  2. seaborn可视化水平箱图并添加抖动数据点(Horizontal boxplot with jittered points in Python)
  3. Git 推送到远程仓库
  4. java输出栈的弹出序列_剑指offer:栈的压入、弹出序列(Java)
  5. Pandas 表连接(Merge,join,concatenate)
  6. 机器学习中的分类模型和回归模型
  7. java awt run_JAVA教程 第五讲 AWT图形用户界面设计(一)
  8. 【CF1342D】Multiple Testcases(贪心+优先队列)
  9. 达观数据郭权:用好ngResource和postman,提高你的开发调试效率
  10. html中怎么设置一个籍贯选择,行政区、籍贯选择标签
  11. 计算机打印怎么取消,电脑怎么取消打印任务_打印机取消打印任务的详细步骤...
  12. 比较全的机油教程(android)
  13. 电力自动化系统为何需要卫星同步时钟装置?
  14. 酷比魔方iwork1x i30双系统版,重装单Ubuntu 20.04系统记录(重力传感器、触摸屏、启动菜单、声卡问题、优化充电慢)
  15. caj 格式的文件转换为word
  16. java 仿易企秀_鲁班H5(开源可视化搭建系统, 可以理解为开源版本易企秀)核心实现原理解析...
  17. 极速office(Word)怎么删除页眉
  18. Firefox如何给页面权限
  19. 华为服务器图片无法显示,服务器的图片无法显示
  20. Android Volley核心源码解析

热门文章

  1. 基础排序算法(冒泡排序,选择排序,插入排序)
  2. [每日一题] OCP1z0-047 :2013-08-14 如何理解USING INDEX?...................................41
  3. scau 2012新生赛 G只有神知道的世界
  4. 201621123048《Java程序设计》第八周学习总结
  5. C#正则表达式:移除文本中所有方括号和其中的内容
  6. “XXXXX” is damaged and can’t be opened. You should move it to the Trash 解决方案
  7. 4项无线技术对比:谁更适合物联网领域?
  8. javascript动画系列第一篇——模拟拖拽
  9. IOS--常用类--NSArray
  10. Autodesk Infrastructure Map Server(AIMS)/MapGuide API 培训材料--第1章