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. 阿里云服务器CentOS6.9 nexus私服配置

    从nexus官网下载nexus版本. 下载地址如下:https://help.sonatype.com/repomanager2/download 如果要下载3.X版本的,可以把repomanager ...

  2. Pots POJ 3414

    /* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...

  3. 基于vue-cli3.11.0创建创建vue项目

    如果电脑已安装vue-cli2.9.6,需要先卸载,然后重装vue-cli3.11.0操作如下 等待几分钟,卸载完成,输入npm install -g @vue/cli 等待安装完成,输入vue -V ...

  4. MariaDB简单操作

    RHEL7之后操作系统带的数据库都是mariadb,跟mysql一样用 1.安装客户端和服务端 [root@localhost ~]# yum install mariadb mariadb-serv ...

  5. request.getPathInfo()的作用

    项目中使用该方法来获取URL,但不知道获取的URL的起始: 如:http://localhost:8080/web_hrs/Action/这里可以是任何东西  ---->request.getP ...

  6. 02·nginx进阶·服务优化配置管理

    企业场景常用的Nginx http功能模块汇总 ngx_ http_ core_ module 包括-些核心的http 参数配置,对应Nginx的配置为HTTP区块部分 ngx_ http _acce ...

  7. Thinkphp5.0终章

    thinkphp5.0最终总结 前期刚开始我是跟着b站上的千峰教育的视频走的,一路上做笔记进行深化与实际操作,中间因为不会开报错,并且视频里面也没有讲到怎么弄报错,因为是新手,那种出错了却不知道错在哪 ...

  8. IDEA 学习笔记之 Console显示日志大小

    Console显示日志大小: IntelliJ IDEA默认的Output输出缓存区大小只有1024KB,超过大小限制的就会被清除,而且还会显示[too much output to process] ...

  9. MongoDB 学习笔记之 mongo-connector安装

    mongo-connector安装: https://github.com/mongodb-labs/mongo-connector 1. yum -y install openssl* 2.编译安装 ...

  10. MongoDB 学习笔记之 删除数据,集合,数据库

    删除数据,集合,数据库: 删除一个文档: db.media.deleteOne({"name": "Sky"}) 删除多个文档: db.media.delete ...