簡體   English   中英

從 JavaScript web 頁面啟動 shell 腳本文件

[英]Launching a shell script file from a JavaScript web page

我有一個網站,我在 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);
    }
});
    }

當我單擊“啟動”按鈕從“/var/www/html/mytest/TestAPI.sh”啟動腳本時,沒有任何反應。 我錯了,或者有其他方法可以執行該腳本,也許使用 PHP?!

我知道這似乎是從 web 頁面啟動腳本的安全漏洞,但這是在用戶登錄后在受控環境中完成的。 歡迎任何想法...

據我所知,process.exec() 是 node.js 代碼.. 它不在瀏覽器上運行,與瀏覽器 Javascript 和 Node.Js 有區別

兩者可能使用相同的語言,但它們運行的環境不同。

因此,如果“/var/www/html/mytest/TestAPI.sh”文件在您的服務器上,您可以使用 PHP 甚至 nodeJS 來運行它。 您只需要創建一個運行該腳本的端點。

如果你打算使用 PHP 那么我認為你可以使用shell_exec()來實現這一點。

我的問題的解決方案是調用執行我的腳本的 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 腳本(我調用了許多程序和其他腳本)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM