我有一個網站,我在 JS 中創(chuàng)建了一個簡單的表單: Launch form 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); }}); }當我單擊“啟動”按鈕從“/var/www/html/mytest/TestAPI.sh”啟動腳本時,沒有任何反應。我哪里錯了,或者有其他方法可以執(zhí)行該腳本,也許使用 PHP?!我知道這似乎是從網頁啟動腳本的安全漏洞,但這是在用戶登錄后在受控環(huán)境中完成的。歡迎任何想法...
2 回答

不負相思意
TA貢獻1777條經驗 獲得超10個贊
我的問題的解決方案是調用一個執(zhí)行我的腳本的 PHP 頁面:javascript:
location.href = "test.php";
php 頁面(test.php):
<?php
$outcome = shell_exec('/var/www/html/mytest.sh 2>&1');
echo $outcome;
?>
但是,為了能夠運行該腳本,您必須將 selinux 配置為“允許”。有很多安全方法可以代替將 Web 服務器安全性降低到“寬松”但不適用于我的 shell 腳本(我調用了許多程序和其他腳本)。

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
據(jù)我所知,process.exec() 是一個 node.js 代碼。它不在瀏覽器上運行,與瀏覽器 Javascript 和 Node.Js 不同
兩者可能使用相同的語言,但它們運行的環(huán)境不同。
因此,如果“/var/www/html/mytest/TestAPI.sh”文件在您的服務器上,您可以使用 PHP 甚至 nodeJS 來運行它。您只需要創(chuàng)建一個運行該腳本的端點。
如果您打算使用 PHP,那么我認為您可以使用shell_exec()來實現(xiàn)這一點。
- 2 回答
- 0 關注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消