自己编写的Shell
shell文件
#!/bin/sh
#
# param 1: log string
#下面的$1指的是调用这个function时传过来的第一个参数,依次类推 $2第二个 $3第三个
funcLog() {
echo "$1\r"
}
funcPrintUsage() {
funcLog "+--------------------------------------------------------------+"
funcLog "|Manual usage: $0 <database user> <database password> <reportDate DD/MM/YYYY> <reportTime HH24:MI>"
funcLog "|"
funcLog "|Example: $0 qcb_user qcb_password 15/02/2016 01:30"
funcLog "+--------------------------------------------------------------+"
}
#引入下面这个shell脚本
. /qcb/uxprod/java/bin/env.sh
#定义变量,以供下面使用
BATCH_CONFIG_DIR=/qcb/uxprod/java/cfg/
BATCH_CONFIG_FILE=${BATCH_CONFIG_DIR}/batch.properties
SYS_CONFIG_FILE=${BATCH_CONFIG_DIR}/system.properties
EMAIL_FOUND_FILE=${BATCH_CONFIG_DIR}/emailfund.properties
MAIL_CONFIG_FILE=${BATCH_CONFIG_DIR}/mail.properties
SACNING_IP_FILE=${BATCH_CONFIG_DIR}/batchanonymousIP.properties
RUNDATE_FILE=/project/QCB/java/rundate
#导入jar包
CLASSPATH=/qcb/uxprod/java/lib:/qcb/uxprod/java/lib/ojdbc6.jar:/qcb/uxprod/java/lib/freemarker-2.3.8.jar:/qcb/uxprod/java/lib/mail.jar
CLS_NAME=com.excelhk.ibank.report.IpScanningReport
# Config multiple login attempts within minutes
TIME_IPSCAN=5
# Config time range of data to scan hourly (60 minutes)
#定义变量TIME_SAMEIP_DIFFACCT,赋值60,注意:等号左右没有空格
TIME_SAMEIP_DIFFACCT=60
#定义变量REPORT_TIME,赋值为空
REPORT_TIME=NIL
REPORT_DAY=NIL
REPORT_TEMP_DAY=NIL
DB_USER=NIL
DB_PWS=NIL
RunMode=NIL
SYSTEM_PROP=NIL
###################
## End config
###################
# $#的意思是putty传给shell的脚本个数
case $# in
# 3|4)三个或四个
3|4)
#把第一个参数赋值给DB_USER这个变量
DB_USER=$1
DB_PWS=$2
#不太清楚这句话什么意思
SYSTEM_PROP="-Dbatch.prop=${BATCH_CONFIG_FILE} -Dmail.prop=${MAIL_CONFIG_FILE} -Ddb.user=${DB_USER} -Ddb.password=${DB_PWS}"
RunMode=11111
REPORT_TIME="01:28:42"
#拿到RUNDATE_FILE这个变量制定文件的内容,赋值给REPORT_DAY
REPORT_DAY=`cat $RUNDATE_FILE`
# Running Daily
#如果参数个数等于3
if [ $# = 3 ]; then
RunMode=11101
REPORT_TIME="$3 00:00:00"
fi
# Running Hourly
#如果参数个数等于4
if [ $# = 4 ]; then
RunMode=11110
#REPORT_TIME接受参数3和参数4的赋值,实际是两个参数,
#比如shell给java的main方法传一个参数,args数组实际接受了两个$3 $4
REPORT_TIME="$3 $4:00"
REPORT_DAY=$4
fi
;;
*)
funcPrintUsage
exit 1
;;
esac
###################
## Main progress
###################
date
$_JAVA_PATH_/bin/java -Xms64m -Xmx256m ${SYSTEM_PROP} -classpath $CLASSPATH ${CLS_NAME} ${EMAIL_FOUND_FILE} ${RunMode} ${SACNING_IP_FILE} ${TIME_IPSCAN} ${TIME_SAMEIP_DIFFACCT} ${REPORT_DAY} ${REPORT_TIME} ${SYS_CONFIG_FILE}
echo ""
date
echo ipScan.sh ends..............
shell调用的java文件
package com.excelhk.ibank.report;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.excelhk.ibank.appobj.CommonDisplay;
import com.excelhk.ibank.util.Db;
import com.excelhk.ibank.util.DbConnection;
import com.excelhk.ibank.util.MailFunc;
import com.excelhk.ibank.util.SysConfig;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class IpScanningReport {
private static Properties prop = null;
private static String pathPrefix = "/mail";
private static Map<String,Template> tempMap = new HashMap<String,Template>();
private static String tempName = "batchIpScan.ftl";
private static String encoding = "utf-8";
//private static String mailPath = "mail/emailfund.properties";
private static IPScanReport ipReportDAO = new IPScanReport();
private static Configuration conf = new Configuration();
public static void main(String[] args) throws Exception {
System.err.println("IpScanningReport main method Start!");
/*
if(args.length != 7) {
for (String string : args) {
System.out.println(string);
}
System.err.println("Usage: IpScaning <batchCfgFile> <RunMode> <scanIpFile> <timeIpScan> <timeDuration> <reportDate> <reportTime> <");
System.exit(0);
}
*/
System.out.println("shell to java argument begin!");
String mailPath = args[0];//batch.properties
System.out.println("args[0] mailPath"+mailPath);
//String mailPath = "/qcb/uxprod/java/cfg/emailfund.properties";
//String mailPath = "D:\\jason\\QCB\\workspace\\QCBWeb\\src\\mail\\emailFund.properties";
String runMode = args[1];
System.out.println("args[1] runMode: "+runMode);
//String runMode = "11111";
String scanIpFile = args[2];//IpScan.properties
System.out.println("args[2] scanIpFile: "+scanIpFile);
//String scanIpFile = "D:\\jason\\QCB\\workspace\\QCBWeb\\src\\mail\\batchanonymousIP.properties";
//String scanIpFile = "/qcb/uxprod/java/cfg/batchanonymousIP.properties";
String time_ipscan = args[3];//ipscanʱ
System.out.println("args[3] time_ipscan: "+time_ipscan);
//String time_ipscan = "5";
String time_sameip_diffacct =args[4];
System.out.println("args[4] time_sameip_diffacct: "+time_sameip_diffacct);
//String time_sameip_diffacct = "60";
String report_date_temp=args[5];
System.out.println("args[5] report_date_temp: "+report_date_temp);
String report_date = CommonDisplay.getDateFmt(report_date_temp);
System.out.println("args[5] report_date: "+report_date);
//String report_date="24/01/2019";
//String report_date="16/01/2019";
String report_times=args[6];
System.out.println("args[6] report_times: "+report_times);
//String report_times="09:28:42";
//String report_times="03:03:03";
String report_time=report_date+" "+report_times;
System.out.println("report_date+report_times: "+report_time);
// String systemPath=args[7];
String systemPath="/qcb/uxprod/java/cfg/system.properties";
System.out.println("args[7] systemPath: "+systemPath);
FileInputStream lfis_Cfg = new FileInputStream(systemPath);
Properties vprp_SysProperty=new Properties();
vprp_SysProperty.load(lfis_Cfg);
SysConfig.init(vprp_SysProperty);
Db.init();
System.out.println("shell to java argument end!");
char [] arr=runMode.toCharArray();
String scanIps = getScanIPs(scanIpFile);
List<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();
//get Daily scan dada give by ip(Daliy)
if(arr[0]=='1'){
String type="Daily";
List<HashMap<String, Object>> listData1 =null;
listData1 = ipReportDAO.getAnonymousIpInformation(scanIps, type, report_time,time_ipscan);
// listData1 = getListForTesting();
for(HashMap hashMap:listData1){
listData.add(hashMap);
}
}
//(Minutely)
if(arr[1]=='1'){
String type="Minutely";
List<HashMap<String, Object>> listData1 =null;
listData1 = ipReportDAO.getAnonymousIpInformation(scanIps, type, report_time,time_ipscan);
for (HashMap<String, Object> hashMap : listData1) {
listData.add(hashMap);
}
}
if (arr[2]=='1') {
List<HashMap<String, Object>> listData2 = ipReportDAO.getSameAccountMultipleIP(report_time);
for (HashMap<String, Object> hashMap : listData2) {
listData.add(hashMap);
}
}
if(arr[3]=='1'){
List<HashMap<String, Object>> listData2 = ipReportDAO.getSameAccountMultipleIP(report_time, time_ipscan, time_sameip_diffacct);
for (HashMap<String, Object> hashMap : listData2) {
listData.add(hashMap);
}
}
if (arr[4]=='1') {
List<HashMap<String, Object>> listData3 = ipReportDAO.getSameIPMultipleAccount(report_time, time_ipscan, time_sameip_diffacct);
for (HashMap<String, Object> hashMap : listData3) {
listData.add(hashMap);
}
}
if (listData.size()>0) {
//InputStream inputStream = IpScanningReport.class.getClassLoader().getResourceAsStream(mailPath);
InputStream inputStream = new FileInputStream(mailPath);
conf.setClassForTemplateLoading(IpScanningReport.class, pathPrefix);
try {
prop = new Properties();
prop.load(inputStream);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Writer writer = new StringWriter(2048);
Template template = getTemplate(tempName);
template.setEncoding(encoding);
Map<String, Object> data = new HashMap<String, Object>();
data.put("listData", listData);
template.process(data, writer);
String textMsg = writer.toString();
MailFunc.sendEmailWithTempalte(prop, textMsg);
}
}
public static Template getTemplate(String tempName) throws IOException{
if(tempMap.containsKey(tempName)){
return tempMap.get(tempName);
}else{
Template template = conf.getTemplate(tempName);
tempMap.put(tempName, template);
return template;
}
}
private static String getScanIPs(String scanIpFile)throws IOException, FileNotFoundException {
Properties rejectCodeProp = new Properties();
rejectCodeProp.load(new FileInputStream(scanIpFile));
String scanIps = "'";
int count = Integer.parseInt(rejectCodeProp.getProperty("Count"));
for (int i = 1; i <= count; i++) {
if(i > 0){scanIps = scanIps + ",'";}
scanIps = scanIps + rejectCodeProp.getProperty("IP"+i);
scanIps = scanIps + "'";
}
return scanIps;
}
}