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);
}
}

java 调用启动远程shell脚本,启动spark的更多相关文章

  1. 【原】Java程序调用远程Shell脚本

    此程序的目的是执行远程机器上的Shell脚本. [环境参数]远程机器IP:192.168.234.123用户名:root密码:rootShell脚本的路径:/home/IFileGenTool/Bak ...

  2. spring boot 以jar的方式启动常用shell脚本

    用spring boot框架做的项目,将第三方包全部打在jar里面,通过shell脚本启动和停止服务,常用的shell脚本模板如下: #!/bin/bashJAVA_OPTIONS_INITIAL=- ...

  3. Linux部署项目 shell脚本启动 及 Centos7开放指定端口

    我们首先要在linux上安装好jdk   tomcat   mysql  这些基本环境,这些可以在楼主的  Linux入门   里面找到. linux部署spring项目 1. 右击项目,maven ...

  4. java classpath批量设置shell脚本

    java classpath批量设置shell脚本   注意:linux bash jar文件之间的分隔符是':'    export JAR_HOME=path to directory which ...

  5. 远程shell脚本执行工具类

    /** * 远程shell脚本执行工具类 */public class RemoteShellExecutorUtils { private static final Logger logger = ...

  6. Linux下启动和停止Java应用程序的Shell脚本

    转自:http://blog.csdn.net/jadyer/article/details/7960802 资料参考来源自兔大侠,并略作修改:http://www.tudaxia.com/archi ...

  7. 编写通用shell脚本启动java项目,适用于多数服务,只需修改服务名即可

    文件名:service-user.sh 文件内容: ##shell脚本的头文件必须有#!/bin/sh ##再次配置java环境变量以防报其他错误## java env#jdk安装目录export J ...

  8. 简易shell脚本启动jar包

    可参考博客: Shell脚本中$0.$?.$!.$$.$*.$#.$@等的意义    https://blog.csdn.net/csgd2000/article/details/80396996 s ...

  9. linux下shell脚本启动jar包

    本文采用的jar包是通过idea下maven打包的springboot项目. 写这个shell脚本是为了在linux下方便启动jar包时不用输入太多的shell命令,将启动脚本的一系列shell命令整 ...

随机推荐

  1. .net core中使用Bumblebee架设微服务网关

    Bumblebee是款基于.net core开发开源的http服务网关,经过最近版本的完善在功能足以满足作为微服务网关的需要.在微服务网关功能中它提供了应用服务负载,故障迁移,安全控制,监控跟踪和日志 ...

  2. OKR群:为什么说每个程序员都应该有自己的个人OKR

    个人OKR OKR,即Object and Key Result,是IT大厂最近争相推广的目标管理工具,例如腾讯.百度和头条(字节跳动). 其实,OKR并不是仅仅只适用于公司和部门内部,我们个人也可以 ...

  3. 洛谷:P5072 [Ynoi2015]盼君勿忘

    原题地址:https://www.luogu.org/problem/P5072 题目简述 给定一个序列,每次查询一个区间[l,r]中所有子序列分别去重后的和mod p 思路 我们考虑每个数的贡献.即 ...

  4. 浅拷贝&深拷贝的对比

    js中两种数据类型 浅拷贝:拷贝就是拷贝指向对象的指针,意思就是说:拷贝出来的目标对象的指针和源对象的指针指向的内存空间是同一块空间,浅拷贝只是一种简单的拷贝,让几个对象公用一个内存,然而当内存销毁的 ...

  5. Angular2+之使用FormGroup、FormBuilder和Validators对象控制表单(取值、赋值、校验和是否可编辑等)

    1.要使用Angular自带的表单控制需要先引入相关模块(.ts文件): import { FormGroup, //表单对象类 FormBuilder, //表单生成工具类 Validators} ...

  6. Joomla 3.0.0 -3.4.6远程代码执行(RCE)漏洞复现

    Joomla 3.0.0 -3.4.6远程代码执行(RCE)漏洞复现 一.漏洞描述 Joomla是一套内容管理系统,是使用PHP语言加上MYSQL数据库所开发的软件系统,最新版本为3.9.12,官网: ...

  7. redis安装详细

      1.cd命令到你的下载文件夹下 2.输入命令下载redis ,可以自己选择下载的路径,redis表示版本号 wget http://download.redis.io/releases/redis ...

  8. PowerSploit-CodeExecution(代码执行)脚本渗透实战

    首先介绍一下国外大牛制作的Powershell渗透工具PowerSploit,上面有很多powershell攻击脚本,它们主要被用来渗透中的信息侦察.权限提升.权限维持. 项目地址:https://g ...

  9. Linux的一些常用命令(一)

    linux 快捷键1.ls 列出本地址上文件, -a 列出所有(包括隐藏文件) -l 按照列表方式显示 -t 按照时间方式排序 2.touch 创建文件 3.  echo 'abc' > 文件名 ...

  10. php注释的作用是什么?

    php注释的作用 1.解释代码功能: 2.调试程序. 说明:在代码中进行注释是很有必要的,规范的注释使的源代码更易于人类理解,可以帮助我们理解别人或者自己以前编写的代码. php怎么添加注释? 1.用 ...