oss文件检测脚本

#!/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;
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值