自己编写的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;
}
}
自己编写的Shell的更多相关文章
- Linux如何编写自启动shell脚本
1.需求分析 在很多情况下,程序员都做着重复枯燥的工作,虽然这些工作也是必须的,其实这些重复性的工作可以执行脚本替代:今天笔者就如何编写自启动shell脚本减少程序员开启服务器后的环境开启工作: 2. ...
- linux c语言编写一个shell壳
目的:我们要用c语言编写一个shell可以运行在linux机器上的. 介绍:shell所在的层次 我们要做的是操作系统,用于用户与操作系统进行交互的myhsell 思路:用户输入 一行字符串,我们先 ...
- 1.编写一个shell脚本
一.shell和shell脚本 在linux系统下,以 #/bin/bash开头的文本会被shell解释器进行解释. shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操 ...
- 使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本
1.删除机器上原有的redis2.8 关闭redis-server killall -9 redis-server 查找redis文件所在目录 which redis 删除相关文件 rm -rf re ...
- 170509、文本编辑器编写的shell脚本在linux下无法执行的解决方法
今天碰到一个奇怪的问题,编写好的shell脚本再linux上执行一直提示找不到文件或目录,后来想想是文本编辑器的问题,记录下来!!! 1.查看当前文本格式 Notepad++界面中,在右下角有文件格式 ...
- 关于windows下编写的shell脚本在linux下无法运行报错问题
首先,你写的shell脚本必须是正确的, 其次,无法运行的原因:因为windows下的换行是两个字节,而你上传到linux,linux下换行是两个字节,所以编译的酒不正确的,导致无法 运行脚本, 这种 ...
- 编写通用shell脚本启动java项目,适用于多数服务,只需修改服务名即可
文件名:service-user.sh 文件内容: ##shell脚本的头文件必须有#!/bin/sh ##再次配置java环境变量以防报其他错误## java env#jdk安装目录export J ...
- Windows编写的shell脚本,在linux上无法执行
前两天由于要查一个数据库的binlog日志,经常用命令写比较麻烦,想着写一个简单的脚本,自动去刷一下数据库的binlog日志,就直接在windows上面写了,然后拷贝到linux中去运行,其实很简单的 ...
- 编写一个shell脚本来编译并运行java代码
概述 编译和运行java分别要用到javac命令和java命令,虽然可以使用IDE(比如eclipse,InteliJ,NetBean...),按一下快捷键就可以实现编译并运行,但是,在之前还要配置一 ...
随机推荐
- scala中的isInstanceOf和asInstanceOf
如果实例化了子类的对象,但是将其赋予了父类类型的变量, 在后续的过程中,又需要将父类类型的变量转换为子类类型的变量,应该如何做? Ø 首先,需要使用isInstanceOf 判断对象是否为指定类的对 ...
- Linux常用基本命令:三剑客命令之-awk 三元表达式
awk 3元表达式,if...else结构都可以用3元表达式改写 ghostwu@dev:~/linux/awk$ awk -v FS=":" '{ type=$3>=100 ...
- MySQL主从及主主环境部署
主从同步 主机环境 mysql的安装可以参考:https://www.cnblogs.com/brianzhu/p/8575243.htmlCentos7版本master:192.168.192.12 ...
- 在centos7 上部署 vuepress
vuepress是一款十分优秀简洁的文档生成器,可以根据目录下的md文档自动生成对应的html文件,界面简洁大方.每一个由 VuePress 生成的页面都带有预渲染好的 HTML,也因此具有非常好的加 ...
- IO事件驱动模型
1:IO事件驱动模型简介 通常,我们写服务器处理模型的程序时,有以下几种模型: (1)每收到一个请求,创建一个新的进程,来处理该请求: (2)每收到一个请求,创建一个新的线程,来处理该请求: (3)每 ...
- 阿里云 centos7 django + uWSGI+Nginx + python3 部署攻略
centos7+nginx+python3+django+uwsgi配置Django 项目部署 1.租的服务器(选择centos)的话,需要在阿里云后台控制台开放几个端口,克隆一下已开放的端口,t ...
- MySQL修改编码为UTF-8无效果解决办法
本来这是一件很简单的事,有很多博客里都有教程,但却足足花了我半天的时间才解决问题. 可能是因为我的MySQL安装时没有选择默认路径的原因,按照网上的教程修改了下图中的my.ini配置文件后编码并没有发 ...
- sqlserver 2017 docker安装(启动代理)
从 Docker Hub 中拉出 SQL Server 2017 Linux 容器映像. docker pull microsoft/mssql-server-linux:2017-latest 运行 ...
- [20180614]删除bootstrap$记录无法启动2.txt
[20180614]删除bootstrap$记录无法启动2.txt --//前几天看链接http://www.xifenfei.com/2018/05/willfully-delete-bootstr ...
- [20171206]rman与truncate.txt
[20171206]rman与truncate.txt --//昨天下班在回家的路上,突然想起以前遇到的问题,就是truncate表后,rman做备份时会备份多少truncate表的信息,--//当时 ...