【问题标题】:Need to execute a Shell Script from Angular JS UI需要从 Angular JS UI 执行 Shell 脚本
【发布时间】:2014-12-22 08:26:45
【问题描述】:

我需要使用 Angular JS 在单击按钮时执行 shell 脚本,谁能告诉我如何实现?

在执行之前,我需要从 GUI/UI 向该 shell 脚本传递一些输入(参数、参数)。

【问题讨论】:

  • shell 脚本是位于托管网页的服务器上还是位于客户端计算机上?
  • 您将通过 AJAX 调用 API,该 API 将为您代理执行脚本并返回输出

标签: javascript angularjs node.js shell shellexecute


【解决方案1】:

这可以通过 node.js 来完成。在 nodejs 中创建一个休息端点并从 angular.Say 中调用它:

$http.get(URL + "/script").then(function (req,response) {
...
write your code here
...    
}

在 node.js 中使用以下代码 sn-ps。

var sys=require('util');
var exec=require('child_process').sys;
var child;

//休息结束点

app.get('/script', function (req, res) {

child = exec("pwd", function (error, stdout, stderr) {
      sys.print('stdout: ' + stdout);
      sys.print('stderr: ' + stderr);
      if (error !== null) {
         console.log('exec error: ' + error);
                      }
    });

  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2013-06-13
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多