How do I run the system commands in javascript?

后端 未结 5 815
执念已碎
执念已碎 2020-12-06 06:08

I need to list all the files in the javascript such as \"ls\"??

相关标签:
5条回答
  • 2020-12-06 06:38

    Please give more information of your environment.

    Unprivileged JavaScript in a browser can neither list files nor execute programs for security reasons.

    In node.js for example executing programs works like this:

    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()

    0 讨论(0)
  • 2020-12-06 06:42

    The short answer is - you should NOT do this as it opens a huge attack vector against your application. Imagine someone running "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.

    0 讨论(0)
  • 2020-12-06 06:45

    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.

    0 讨论(0)
  • 2020-12-06 06:53

    An even easier way in node.js is:

    var fs = require('fs');
    var ls = fs.readdirSync('/usr');
    

    The variable ls now contains an array with the filenames at /usr.

    0 讨论(0)
  • 2020-12-06 06:55

    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

    0 讨论(0)
提交回复
热议问题