2

I need to be able to execute a shell command through javascript, similar to the php function "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.

Users make a request to the server for some data, and the server-side javascript code is called. From the javascript file, I need to execute a program in order to gather data based on user input, then pass this data back.

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.

MashterYoda
  • 118
  • 9

3 Answers3

1

JavaScript has no 'exec' function like PHP does. It's all because JavaScript runs on the client and don't have access to the server part.

However you can create PHP page and send AJAX requests to it to execute particular command. 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!

volter9
  • 737
  • 5
  • 16
0

This might be what you're looking for: 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

Todd R
  • 18,236
  • 8
  • 31
  • 39
0

If it's client side - You can't

If it's node.js:

var exec = require('child_process').exec;
child = exec("command", function (error, stdout, stderr)
 {
   // handle the output
});
Krzysztof Wende
  • 3,208
  • 25
  • 38