如何通过java远程调用linux命令或shell脚本

利用java远程调用linux命令或shell脚本

目的:远程备份、导出/导入数据库数据、执行定时任务等等。

当然要根据自己特定功能需求。

1、 远程主机

这里使用我们的阿里云来作为测试服务器

版本如下:

用户名:odysee

密码:...隐藏...

脚本:/home/odysee/bb/available.sh

功能:获取/可用空间,顺便传一个位置参数(虽然这里用不到)。

这里大家根据自己需求来编写脚本。

2、 Maven项目添加依赖jar包

<!-- ssh2 -->

<dependency>

<groupId>org.jvnet.hudson</groupId>

<artifactId>ganymed-ssh2</artifactId>

<version>build210-hudson-1</version>

</dependency>

3、 编写工具类ShellExecutorUtil

关于以下代码,请大家详看注释。

定义变量及构造器

登录方法

将linux返回屏幕数据通过流返回为字符串

以上都是一些通用方法

下面是主要实现方法

4、 测试

赋予脚本可执行权限

运行代码

5、 补充

工具类代码贴在最后,原文件可以留言或私信。

以上只是实现java调用linux脚本的一种方式,如果大家有其他的方法。

欢迎评论留言,感谢支持。

工具类如下:

package com.qzsoft.qzhrmis.commons.utils;

import java.io.IOException;

import java.io.InputStream;

import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;

import ch.ethz.ssh2.ChannelCondition;

import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;

import ch.ethz.ssh2.StreamGobbler;

public class ShellExecutorUtil {

private Connection conn;

private String ip;

private String username;

private String password;

//字符集

private String charset = Charset.defaultCharset().toString();

//超时30minute

private static final int TIME_OUT = 1000 * 30 * 60;

/**

* 构造器

* @param ip

* @param username

* @param pasword

*/

public ShellExecutorUtil(String ip, String username, String pasword) {

this.ip = ip;

this.username = username;

this.password = pasword;

}

/**

* 登录

* @return

* @throws IOException

*/

private boolean login() throws IOException {

conn = new Connection(ip);

conn.connect();

return conn.authenticateWithPassword(username, password);

}

/**

* 使用流处理执行linux命令输出到屏幕的数据

* @param in

* @param charset

* @return

* @throws Exception

*/

private String processStream(InputStream in, String charset) throws Exception {

byte[] buf = new byte[1024];

StringBuilder sb = new StringBuilder();

while (in.read(buf) != -1) {

sb.append(new String(buf, charset));

}

return sb.toString();

}

/**

* 执行脚本

* @param cmds linux命令 or shell脚本

* @return

* @throws Exception

*/

public int exec(String cmds) throws Exception {

//linux中标准输出

InputStream stdOut = null;

//linux中标准错误输出

InputStream stdErr = null;

String outStr = "";

String outErr = "";

//linux命令返回值:即$?

int ret = -1;

try {

if (login()) {

//开启一个会话

Session session = conn.openSession();

//执行命令

session.execCommand(cmds);

//获取一个标准输出

stdOut = new StreamGobbler(session.getStdout());

//通过通用方法转化成字符串

outStr = processStream(stdOut, charset);

//获取一个标准错误输出

stdErr = new StreamGobbler(session.getStderr());

//通过通用方法转化成字符串

outErr = processStream(stdErr, charset);

//设置超时

session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);

//命令返回值 $?

ret = session.getExitStatus();

} else {

throw new Exception("登录"+ip+"失败!");

}

} finally {

if (conn != null) conn.close();

IOUtils.closeQuietly(stdOut);

IOUtils.closeQuietly(stdErr);

}

System.out.println("outStr=" + outStr);

System.out.println("outErr=" + outErr);

System.out.println("ret=" + ret);

return ret;

}

public static void main(String args[]) throws Exception {

ShellExecutorUtil executor = new ShellExecutorUtil("IP地址", "用户名", "密码");

System.out.println(executor.exec("脚本 位置参数或者命令)"));

}

}

举报
评论 0