我不懂前端脚本,需要一些关于制作javascript的帮助。
使用谷歌,我取得了一些进展,但无法达到当时要求的结果。
我有shell脚本,它正在按预期工作。现在,我需要为用户创建一个简单的UI,他们将在其中输入报告的start_date和end_date,这需要获取并传递给shell脚本。
我读到,不可能从javascript直接调用shell脚本,所以我调用简单的PHP文件,它反过来执行shell脚本。
到目前为止,我的javascript可以执行这些操作,并且shell脚本正在执行。
我需要添加获取start_date和end_date的功能,重要的是将其转换为unix tick格式并将其传递给shell脚本。
这是我的脚本
javascript
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.4.5/jquery-ui-timepicker-addon.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.4.5/jquery-ui-timepicker-addon.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datetimepicker1,#datetimepicker2" ).datetimepicker();
});
</script>
<script type="text/javascript">
function submitAction() {
$.ajax({
url:"report.php",
type: "POST"
})
}
</script>
</head>
<body>
<p>Start Date: <input type="text" id="datetimepicker1"></p>
<p>End Date: <input type="text" id="datetimepicker2"></p>
<p><input type="button" onclick="submitAction()" value="Get Report" /></p>
</body>
</html>
php
<?php
$output = exec("./exceptions_report.sh 1427955771711 1427998971711");
echo 'Report will be sent to your mail';
?>
外壳
startdate=$1
enddate=$2
curl -X POST {http://localhost:9200/_search?} -d '{"query":{"filtered":{"query":{"bool":{"should":[{"query_string":{"query":"*"}}]}},"filter":{"bool":{"must":[{"range":{"@timestamp":{"from":$startdate,"to":$enddate}}},{"terms":{"level.raw":["ERROR"]}}]}}}},"size":1900,"fields":["logdate","level","device_id","thread","category","messagetext"],"sort":[{"@timestamp":{"order":"asc","ignore_unmapped":true}},{"@timestamp":{"order":"asc","ignore_unmapped":true}}]
}' > input.json
#
cat input.json | jq '.hits.hits[].fields' --compact-output | sed 's/[][]//g' | recs tocsv --key logdate,level,device_id,thread,category,messagetext > report.csv
#
mail -s "Exceptions report" -a report.csv "mail_adress" << EOM
Please find exceptions report attached.
EOM
#
在html中添加一个表单,如下所示:
<form id="form" action="" method="POST">
<p>Start Date: <input type="text" name="date_1" id="datetimepicker1"></p>
<p>End Date: <input type="text" name="date_2" id="datetimepicker2"></p>
<p><input type="submit" value="Get Report" /></p>
</form>
<div id="AjaxResult"></div>
<script type="text/javascript">
$(document).ready(function(){
$('#form').on('submit', function(e){
e.preventDefault();
data = $(this).serialize();
$.ajax({
url:"report.php",
type: "POST",
data: data,
success: function(data){
$('#AjaxResult').html(data);
}
});
});
});
</script>
- 这里我们使用
.serialize()
作为表单数据 - 并在提交
#form
时激发ajax - report.php的结果将返回到html中的
#AjaxResult
div e.prventDefault()
取消默认表单提交操作阻止页面刷新
在您的PHP(report.PHP)中,您可以获得这样的输入值:
<?php
$date_1 = $_POST['date_1'];
$date_2 = $_POST['date_2'];
echo $date_1 . "<br>";
echo $date_2 . "<br>";
$output = exec("./exceptions_report.sh 1427955771711 1427998971711");
echo 'Report will be sent to your mail';
?>
将输出:
04/08/2015 00:00 // user selection
04/10/2015 00:00 // user selection
Report will be sent to your mail
在成功回调中使用.html()
方法的<div id="AjaxResult"></div>
内部
小的修正,可能对其他人有用。在测试过程中,我发现为了确保变量正确地传递到shell脚本,需要在php中使用escapeshellarg。
$date_1=escapeshellarg($_POST['date_1'])
$date_2=escapeshellarg($_POST['date_2']);