如何使用javascript在网页中获取shell脚本的结果?

时间:2018-01-23 07:55:51

标签: javascript html shell

我正在尝试设置本地服务器来维护本地git存储库。

我已经为命令行实用程序编写了几个shell脚本,它返回了我想要的输出。如果可能的话,我想通过使用javascript从网页运行这些。 (服务器将运行脚本,客户端)

我没有为了在网页上显示信息而编写脚本,但是如果可能的话就想实现它们,以免再次重写代码。

我有这个javascript代码:

test.js

var exec = require('child_process').exec;

function test() {
    exec("ssh git@raspberrypi 'ls ~/Git/'",
      function (error, stdout, stderr) {
        console.log(stdout);
        // document.write(stdout);
      });
}

test()

如果我使用node test.js运行此功能 我得到打印到控制台的命令输出。

  

git_folder1.git

     

git_folder2.git

     

git_folder3.git

但是,如果我取消注释行document.write(stdout),并从HTML调用它,我就没有输出。

的test.html

<html>
    <head>
        <script type="text/javascript" src="test.js"></script>
    </head>
    <body>
        <script>test()</script>
    </body>
</html>

如果我将document.write("Hello")放在exec之前“Hello”写入文档,但如果放在函数的底部(或内部),则不会。{/ p>

我做错了吗? 有没有更好的方法来获取网页中的shell脚本输出?

0 个答案:

没有答案