我有一个网站,我用JS创建了一个简单的表单。启动表单
function()
{
var thePrompt = window.open("", "", "widht=50");
var theHTML = "";
theHTML += "<p>Please insert the IP,TestCaseID and your credentials</p>";
theHTML += "<br/>";
theHTML += "IPprinter: <input type='text' id='theIP' placeholder='Enter Printer IP'/>";
theHTML += "<br/>";
theHTML += "TestCaseID: <input type='text' id='theID' placeholder='Enter TestCase ID'/>";
theHTML += "<br/>";
theHTML += "Username: <input type='text' id='theUser' placeholder='Enter Username'/>";
theHTML += "<br />";
theHTML += "Password: <input type='text' id='thePass' placeholder='Enter Password'/>";
theHTML += "<br />";
theHTML += "<input type='button' value='Launch' id='Launch'/>";
thePrompt.document.body.innerHTML = theHTML;
var theIP = thePrompt.document.getElementById("theIP").value;
var theID = thePrompt.document.getElementById("theID").value;
var theUser = thePrompt.document.getElementById("theUser").value;
var thePass = thePrompt.document.getElementById("thePass").value;
thePrompt.document.getElementById("Launch").onclick = function () {
var process = require('child_process');
process.exec('./var/www/html/mytest/TestAPI.sh',function (err,stdout,stderr) {
if (err) {
console.log("\n"+stderr);
} else {
console.log(stdout);
}
});
}
当我点击 "Launch "按钮从 "varwwwhtmlmytestTestAPI.sh "中启动脚本时,什么都没有发生。我哪里出错了,或者有其他方法来执行该脚本,也许使用PHP?
我知道这似乎是一个从网页上启动脚本的安全漏洞,但这是在用户登录后的受控环境中完成的。欢迎提供任何想法...
据我所知,process.exec()是一个node.js代码......它不在浏览器上运行,浏览器Javascript和Node.Js是有区别的。
两者可能使用相同的语言,但它们运行的环境是不同的。
所以 如果 varwwwhtmlmytestTestAPI.sh "文件在你的服务器上,因此你可以使用PHP甚至nodeJS来运行它,你只需要创建一个运行该脚本的端点。
如果你要使用PHP,那么我认为你可以使用 shell_exec() 来实现这个目标。
解决我的问题的方法是调用一个PHP页面,执行我的脚本:javascript。
location.href = "test.php";
php page(test.php).然而,要运行这个脚本,你必须将selinux配置为 "permissive"。
<?php
$outcome = shell_exec('/var/www/html/mytest.sh 2>&1');
echo $outcome;
?>
然而,为了能够运行该脚本,你必须将selinux配置为 "permissive".有一个安全得多的方法,而不是将web服务器的安全性降低到 "permissive",但对我的shell脚本不起作用(我调用了许多程序和其他脚本)。