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. 前端获取后台传输过来是数据 {张三:12} 解析为[object object],获取其中内容

    昨天遇到前端传输过来的数据为[{张三:12},{李四:23}],后台用的是map格式,我在前端js中暂未找到直接调用对象内容的方法,故利用以下方法来获取: $.each(data.data,funct ...

  2. ibatis 学习

    1.$与#的区别: 用到#和$来获取传的参数值,其中#是将传来的值替换(如果是字符串,会将‘’带着替换,比如上面$value$,用#value#的话,就会出错.#与$的使用区别:$中间的变量就是直接替 ...

  3. .Net Core 商城微服务项目系列(十四):分布式部署携程Apollo构建配置中心

    一.开场白 在系统设计里我们有很多配置希望独立于系统之外,而又能够被系统实时读取.但是在传统的系统设计里,配置信息通常是耦合在系统内的,比如.net里通常会放在App.config或者web.conf ...

  4. 第二篇:php开发工具

    倍,这里为您介绍一些常用的工具. PHP IDE PHP IDE也不少,主要从几个方面进行筛选: 跨平台(能够同时在windows,mac或者ubuntu上面运行) 版本控制(SVN,GIT) 文件历 ...

  5. K8s 从懵圈到熟练 – 镜像拉取这件小事

    作者 | 声东 阿里云售后技术专家 导读:相比 K8s 集群的其他功能,私有镜像的自动拉取,看起来可能是比较简单的.而镜像拉取失败,大多数情况下都和权限有关.所以,在处理相关问题的时候,我们往往会轻松 ...

  6. js模拟下拉菜单-键盘、鼠标(代码详解)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. js完整

    jQuery jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行 ...

  8. Java 多线程爬虫及分布式爬虫架构探索

    这是 Java 爬虫系列博文的第五篇,在上一篇 Java 爬虫服务器被屏蔽,不要慌,咱们换一台服务器 中,我们简单的聊反爬虫策略和反反爬虫方法,主要针对的是 IP 被封及其对应办法.前面几篇文章我们把 ...

  9. Windows 10 更新后VMware Workstation pro无法运行 (无需卸载原版本VM)

    问题 描述:当前Windows版本是win10-1903,VMware版本比较老旧是VMware Workstation Pro 15.0.4:国庆节后微软推送了一个新的更新补丁,10月10日更新之后 ...

  10. 【NOIP2003】传染病控制

    Description 问题背景: 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府 决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种 ...