如何在 javascript 中运行系统命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5321884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run the system commands in javascript?
提问by Ambika
I need to list all the files in the javascript such as "ls"??
我需要列出javascript中的所有文件,例如“ls”??
回答by Hendrik Brummermann
Please give more information of your environment.
请提供有关您的环境的更多信息。
Unprivileged JavaScript in a browser can neither list files nor execute programs for security reasons.
出于安全原因,浏览器中的非特权 JavaScript 既不能列出文件也不能执行程序。
In node.js for example executing programs works like this:
例如在 node.js 中,执行程序的工作方式如下:
var spawn = require('child_process').spawn,
var ls = spawn('ls', ['-l']);
ls.stdout.on('data', function (data) {
console.log(data);
});
And there is a direct way to list files using readdir()
还有一种使用readdir()列出文件的直接方法
回答by JohnP
You can't run system commands on the client with JS since it works inside a browser sandbox. You'd need to use some other client side tech like Flash, ActiveX or maybe Applets
您无法使用 JS 在客户端上运行系统命令,因为它在浏览器沙箱中运行。您需要使用其他一些客户端技术,如 Flash、ActiveX 或 Applets
回答by Kumar
AFAIK, you can not run any system command, this will violate the security model. You can do send a print command but I wonder anything beyond that is possible.
AFAIK,您不能运行任何系统命令,这将违反安全模型。您可以发送打印命令,但我想知道除此之外还有什么可能。
回答by baudot
An even easier way in node.js is:
node.js 中更简单的方法是:
var fs = require('fs');
var ls = fs.readdirSync('/usr');
The variable ls
now contains an array with the filenames at /usr.
该变量ls
现在包含一个文件名位于 /usr 的数组。
回答by Atanas Korchev
The short answer is - you should NOT do this as it opens a huge attack vector against your application. Imagine someone running "rm -rf"
:).
简短的回答是 - 您不应该这样做,因为它会为您的应用程序打开一个巨大的攻击媒介。想象一下有人在跑步"rm -rf"
:)。
If you MUST do this and you are 1000% sure you allow only a few commands which cannot cause any harm you can call a server page using Ajax. That page could run the specified command and return response. Again I emphasize this is a huge security risk and should better NOT be done.
如果您必须这样做并且您 1000% 确定您只允许一些不会造成任何伤害的命令,您可以使用 Ajax 调用服务器页面。该页面可以运行指定的命令并返回响应。我再次强调这是一个巨大的安全风险,最好不要这样做。