博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 调用启动远程shell脚本,启动spark
阅读量:6969 次
发布时间:2019-06-27

本文共 2212 字,大约阅读时间需要 7 分钟。

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

你可能感兴趣的文章
Xamarin XAML语言教程Xamarin.Forms中活动指示器的显示隐藏
查看>>
免费空间上的mysql数据库怎么连接?
查看>>
hdu2222 Keywords Search
查看>>
java 发送post请求参数中含有+会转化为空格的问题
查看>>
tomcat生产部署关键參数设置
查看>>
正则表达式,字符串中需要两个反斜杠“\\d”
查看>>
Java-idea-常用技巧-转maven,解决包依赖冲突
查看>>
统计学中RR OR AR HR的区别
查看>>
vue加百度统计代码(亲测有效)
查看>>
判断Json字符串返回类型 对象 或者 数组
查看>>
SpringCloud2.0入门3-新的eureka依赖
查看>>
Java基础-SSM之mybatis多对多关联
查看>>
Google展示“配方搜索”概念 利用语义搜索学做菜
查看>>
窗体界面设计01
查看>>
IOS开发技巧之──字数统计函数
查看>>
Cocos2d API 解析之Texture2d
查看>>
Java编程中“为了性能”尽量要做到的一些地方
查看>>
C# 使用OLEDB读取不同版本Excel数据的连接字符串
查看>>
设置tomcat启动超时,不会自动停止
查看>>
005商城项目:ssm框架的整合成功之后的问题:使用maven的tomcat插件时的debug
查看>>