提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、创建模板Template.txt
open cebbank
lcd /tmp/
cd 0000000001
newjob
put 1.txt
jobsubmit
close
二、脚本Mftutil.sh
#!/bin/bash
######### 光大代扣脚本 ################
######## autor junfeng ###############
##### $1 传上传或者下载的目录
##### $2 put表示上传 get表示下载
##### $3 文件名称
sed -i '2c lcd '$1'' /root/Axway/Synchrony/SecureClient/Template.txt
sed -i '5c '$2' '$3'' /root/Axway/Synchrony/SecureClient/Template.txt
/root/Axway/Synchrony/SecureClient/sclient script /root/Axway/Synchrony/SecureClient/Template.txt
三、java调用脚本
1.引入库
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
2.执行远程shell脚本代码
代码如下(示例):
public static void main(String[] args) throws Exception {
ExecuteShellUtil ex = ExecuteShellUtil.getInstance();
ex.init("127.0.0.1", 22, "root", "123456");
String result = ex.execCmd("/root/Axway/Synchrony/SecureClient/Mftutil.sh /tmp/ put 1.txt");
System.err.println(result);
}
package com.web.api.util;
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
public class ExecuteShellUtil {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
/** 未调用初始化方法 错误提示信息 */
private static final String DONOT_INIT_ERROR_MSG = "please invoke init(...) first!";
private Session session;
private Channel channel;
private ChannelExec channelExec;
private ExecuteShellUtil() {
}
/**
* 获取ExecuteShellUtil类实例对象
*
* @return 实例
* @date 2019/4/29 16:58
*/
public static ExecuteShellUtil getInstance() {
return new ExecuteShellUtil();
}
/**
* 初始化
*
* @param ip
* 远程Linux地址
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @throws JSchException
* JSch异常
* @date 2019/3/15 12:41
*/
public void init(String ip, Integer port, String username, String password) throws JSchException {
JSch jsch = new JSch();
jsch.getSession(username, ip, port);
session = jsch.getSession(username, ip, port);
session.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect(60 * 1000);
LOGGER.info("Session connected!");
// 打开执行shell指令的通道
channel = session.openChannel("exec");
channelExec = (ChannelExec) channel;
}
/**
* 执行一条命令
*/
public String execCmd(String command) throws Exception {
if (session == null || channel == null || channelExec == null) {
throw new Exception(DONOT_INIT_ERROR_MSG);
}
channelExec.setCommand(command);
channel.setInputStream(null);
channelExec.setErrStream(System.err);
channel.connect();
StringBuilder sb = new StringBuilder(16);
try (InputStream in = channelExec.getInputStream();
InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr)) {
String buffer;
while ((buffer = reader.readLine()) != null) {
sb.append("\n").append(buffer);
}
// 释放资源
close();
LOGGER.info("execCmd result - > " + sb);
return sb.toString();
}
}
/**
* 释放资源
*
* @date 2019/3/15 12:47
*/
private void close() {
if (channelExec != null && channelExec.isConnected()) {
channelExec.disconnect();
}
if (channel != null && channel.isConnected()) {
channel.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
}