简体   繁体   English

Javascript函数,例如PHP exec()

[英]Javascript function like PHP exec()

I need to be able to execute a shell command through javascript, similar to the php function "exec()". 我需要能够通过javascript执行shell命令,类似于php函数“ exec()”。 I understand that this may be impractical in javascript because of security reasons, but my javascript code is running on the server, and no clients have direct access to the file. 我了解由于安全原因,这在javascript中可能是不切实际的,但是我的javascript代码正在服务器上运行,并且没有客户端可以直接访问该文件。

Users make a request to the server for some data, and the server-side javascript code is called. 用户向服务器请求一些数据,然后调用服务器端javascript代码。 From the javascript file, I need to execute a program in order to gather data based on user input, then pass this data back. 我需要从javascript文件执行一个程序,以便根据用户输入收集数据,然后将这些数据传回。

If this isn't possible in vanilla Javascript, please point me towards a library or tool that can do this, preferably in javascript/frameworks on javascript. 如果在原始Javascript中无法做到这一点,请向我指出可以执行此操作的库或工具,最好在javascript / javascript上使用。

JavaScript has no 'exec' function like PHP does. JavaScript没有像PHP那样的“ exec”功能。 It's all because JavaScript runs on the client and don't have access to the server part. 这是因为JavaScript在客户端上运行并且无法访问服务器部分。

However you can create PHP page and send AJAX requests to it to execute particular command. 但是,您可以创建PHP页面并向其发送AJAX请求以执行特定命令。 ALTHOUGH , you need to be VERY, VERY and VERY cautious about which commands to run. 尽管您需要非常,非常和非常谨慎地运行哪些命令。 It's very dangerous to do like that. 那样做非常危险。 I don't advice you to do like that, however, it's possible. 我不建议您那样做,但是有可能。

Good luck! 祝好运!

This might be what you're looking for: https://github.com/arturadib/shelljs 这可能是您要寻找的: https : //github.com/arturadib/shelljs

Or, if you want to have direct access to commands, perhaps this will help: http://nodejs.org/api/child_process.html 或者,如果您希望直接访问命令,则可能会有所帮助: http : //nodejs.org/api/child_process.html

If it's client side - You can't 如果是客户端-您不能

If it's node.js: 如果是node.js:

var exec = require('child_process').exec;
child = exec("command", function (error, stdout, stderr)
 {
   // handle the output
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM