Java代码调用服务器上的Shell脚本
Java代码调用服务器上的Shell脚本
这里主要是因为我们报表平台有用到用户手工录入的数据作为结果数据且需要纳入saiku去展示
如我们所知,saiku不会自动刷新,所以需要在数据更新接口中调用服务器上的shell脚本
话不多说,上代码: (直接使用Eclipse, run Java Application ,执行成功后脚本中的输出内容会打印到控制台)
package saikuDemo1; import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler; import java.io.*; /**
* 远程执行linux的shell script
* @author Ickes
* @author2 hpp
* @since V0.2
*/
public class RemoteExecuteCommand {
//字符编码默认是utf-8
private static String DEFAULTCHART="UTF-8";
private static Connection conn;
private String ip;
private String userName;
private String userPwd; public RemoteExecuteCommand(String ip, String userName, String userPwd) {
this.ip = ip;
this.userName = userName;
this.userPwd = userPwd;
} public RemoteExecuteCommand() { } /**
* 远程登录linux的主机
* @author Ickes
* @since V0.1
* @return
* 登录成功返回true,否则返回false
*/
public Boolean login(){
boolean flg=false;
try {
conn = new Connection(ip);
conn.connect();//连接
flg=conn.authenticateWithPassword(userName, userPwd);//认证
if (flg){
System.out.println("认证成功!");
}
} catch (IOException e) {
e.printStackTrace();
}
return flg;
}
/**
* @author Ickes
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行完后返回的结果值
* @since V0.1
*/
public String execute(String cmd){
String result="";
try {
if(login()){
Session session= conn.openSession();//打开一个会话
session.execCommand(cmd);//执行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
//如果为得到标准输出为空,说明脚本执行出错了
if(result == null){
result=processStdout(session.getStderr(),DEFAULTCHART);
}
conn.close();
session.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
} /**
* @author Ickes
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行成功后返回的结果值,如果命令执行失败,返回空字符串,不是null
* @since V0.1
*/
public String executeSuccess(String cmd){
String result="";
try {
if(login()){
Session session= conn.openSession();//打开一个会话
session.execCommand(cmd);//执行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
conn.close();
session.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
} /**
* 解析脚本执行返回的结果集
* @author Ickes
* @param in 输入流对象
* @param charset 编码
* @since V0.1
* @return
* 以纯文本的格式返回
*/
public static String processStdout(InputStream in, String charset){
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));
String line=null;
while((line=br.readLine()) != null){
buffer.append(line+"\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
} public static void main(String[] args) { RemoteExecuteCommand rec=new RemoteExecuteCommand("10.22.33.44", "root","root");// 参数分别为服务器IP ,用户名,密码
//执行命令
try {
if(rec.login()){ System.out.println("=====第一个步骤=====");
Session session= conn.openSession();//打开一个会话
//TODO:多条命令
session.execCommand("sh /app/Saiku/saikuRefreshShell.sh");//执行命令,这里需要确定刷新脚本存在的绝对路径哦
String result=processStdout(session.getStdout(),DEFAULTCHART);
//如果为得到标准输出为空,说明脚本执行出错了
if(result == null){
System.out.println("脚本出错");
result=processStdout(session.getStderr(),DEFAULTCHART);
}
System.out.println(result);
session.close(); /**
System.out.println("=====第二个步骤=====");
Session session2= conn.openSession();//打开一个会话
//TODO:多条命令
session2.execCommand("cd /home/ubuntu/Desktop/music_rec/user_sim/result;cat xyy_result_m10d.json");//执行命令
String result2=processStdout(session2.getStdout(),DEFAULTCHART);
//如果为得到标准输出为空,说明脚本执行出错了
if(result == null ){//StringUtils.isBlank(result2)
System.out.println("脚本出错");
result2=processStdout(session2.getStderr(),DEFAULTCHART);
}
System.out.println(result2);
session2.close();
*/ conn.close();
}
} catch (IOException e) {
e.printStackTrace();
} } public static void setCharset(String charset) {
DEFAULTCHART = charset;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
Java代码调用服务器上的Shell脚本的更多相关文章
- java调用机器上的shell脚本
java调用机器上的shell脚本,可以这样方便的通过shell脚本调用本机的C.C++等程序 Process process = null; Runtime runTime = Runtime.ge ...
- Java程序调用带参数的shell脚本返回值
Java程序调用带参数的shell脚本返回值 首先来看看linux中shell变量(\(#,\)@,$0,$1,\(2)的含义解释 变量说明: - \)$ Shell本身的PID(ProcessI ...
- java远程执行linux服务器上的shell脚本
业务场景:需要从服务器A中新增的文件同步至本地服务器,服务器A中内存有限,需同步成功之后清除文件. Java调用远程shell脚本,需要和远程服务器建立ssh链接,再调用指定的shell脚本. 1.创 ...
- 通过Jenkins调用自动部署war包及jar包到服务器上的Shell脚本
1)部署war包#!/bin/bashif [ id>0];then echo"stopproject" kill −9 idelse echo "project ...
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...
- java远程调用linux的命令或者脚本
转载自:http://eksliang.iteye.com/blog/2105862 Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar) 使用步骤如下 ...
- linux 执行远程linux上的shell脚本或者命令以及scp 上传文件到ftp--免密码登陆
场景:在linux A 上执行Linux B上的shell脚本和命令 步骤1.设置ssh免登陆 1.SSH无密码登录 # 本地服务器执行(A机器):生成密钥对 ssh-keygen -t dsa -P ...
- Java代码调用Oracle的存储过程,存储函数和包
Java代码调用存储过程和存储函数要使用CallableStatement接口 查看API文档: 上代码: java代码调用如下的存储过程和函数: 查询某个员工的姓名 月薪 职位 create or ...
- windows下上传shell脚本不能运行—将dos模式修改为unix 文件格式
windows下上传shell脚本至linux,其格式将为dos.dos模式的shell脚本将不能再linux下正确运行,需要修改文件模式为unix. 1 查看文件模式方法 linux服务器上,用vi ...
随机推荐
- linux install Openvino
recommend centos7 github Openvino tooltiks 1. download openvino addational installation for ncs2 ncs ...
- bzoj 3704 昊昊的机油之GRST - 贪心
题目传送门 传送门 题目大意 给定一个数组$a$和数组$b$,每次操作可以选择$a$的一个子区间将其中的数在模4意义下加1,问把$a$变成$b$的最少操作次数. 首先求$b - a$,再差分,令这个数 ...
- python开发环境配置和python源码打包生成exe可执行文件
Windows下开发环境准备 1.分别安装:python2和python32.安装Python的集成工具:Anaconda3.安装Pycharm Pycharm设置 设置: File->Sett ...
- MySQL 常用30种SQL查询语句优化方法
1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...
- rem的在vue项目中使用配置,,浏览器的兼容性之Mate标签
在vue中配置rem 位置:在APP.vue的script中,在export default之外 (()=>{ let winW = document.documentElement.clien ...
- 分治(超级easy 不要看)
P1226快速幂 #include<bits/stdc++.h> using namespace std; #define int long long ; int f(int b,int ...
- Sitecore8.2 Solr5.1.0配置步骤
1.首先下载Solr安装包,官方提供了几种下载,我选的的solr的5.1.0版本zip包,下载链接:http://mirror.bit.edu.cn/apache/lucene/solr. 2.下载后 ...
- 剑指offer 08:跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). public class Solution { public int ...
- os.remove异常处理
这种情况,正反斜杠都没问题.(windows环境下) 这种情况会出现下列异常 对于目录的形式,把反斜杠改成正斜杠就好了
- 括号生成(Java实现)
题目: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "(()( ...