#!/bin/bash
#-------------------------host---port--username--pw---文件路径------检测时间间隔分钟--停止时间(HHmm)
#--@resource_reference{"tool-service.jar"}
##@resource_reference{"sftp_datasource"}
while read myline
do
echo "LINE:"$myline
OLD_IFS="$IFS"
IFS=","
arr=($myline)
if test ${arr[0]} = $1;then
ARGS=${arr[1]}
echo ${arr[1]}
fi
IFS=$OLD_IFS
done < sftp_datasource
# ARGS=$@;
echo $ARGS;
java -jar ./tool-service.jar $ARGS $2 $3 $4
if [ $? -eq 0 ]; then
echo "====task exec success!===="
else
echo "====task exec failed!===="
exit 1
fi
package utils;
import com.jcraft.jsch.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class SftpUtils {
private static Session sshSession;
public static void main(String[] args) throws JSchException {
ChannelSftp sftp = sftpConnection(args[0],Integer.valueOf(args[1]),args[2],args[3]);
// ChannelSftp sftp = sftpConnection("120.26.51.197",22,"mysftp","Admin123");
boolean isExist = false;
SimpleDateFormat dateFormat = new SimpleDateFormat("HHmm");
while (sftp!= null && !isExist && Integer.valueOf(dateFormat.format(new Date())) < Integer.valueOf(args[6])){
try {
System.out.println("当前时间:" + dateFormat.format(new Date()));
sftp.get(args[4]);
// sftp.get("/upload/test1.txt");
isExist = true;
} catch (SftpException e) {
System.out.println("文件不存在");
try {
// Thread.sleep(30000);
Thread.sleep(Integer.valueOf(args[5])*1000*60);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
sftpClose(sftp);
sessionClose();
}
/**
* 连接sftp服务器
* @param host ftp地址
* @param port 端口
* @param userName 账号
* @param password 密码
* @return
*/
public static ChannelSftp sftpConnection(String host, int port, String userName, String password) throws JSchException {
JSch jsch = new JSch();
ChannelSftp channelSftp = null;
jsch.getSession(userName, host, port);
sshSession = jsch.getSession(userName, host, port);
sshSession.setPassword(password);
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
sshSession.setConfig(properties);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
return channelSftp;
}
/**
*@description 退出Sftp服务器登录
*@return
**/
public static void sftpClose(ChannelSftp channelSftp){
if (channelSftp != null) {
if (channelSftp.isConnected()){
channelSftp.disconnect();
}
}
}
/**
* 关闭session
*/
public static void sessionClose(){
if (sshSession != null) {
if (sshSession.isConnected()){
sshSession.disconnect();
sshSession = null;
}
}
}
}