简体   繁体   中英

Running Linux Commands From The Web With NodeJS + Real Time Output

Simply put is it possible and what is the best way to achieve being able to send commands to a nodeJS server from a php script(server side stops users from sending custom and damaging commands compared to sending the commands from client side js) to the nodejs server which will then run these commands and send back real time output to the webpage.

What ive been able to come up with so far is sending the commands using php curl to the nodeJS websocket(socket.io) and then the nodeJS server relaying the output via eventsource/socket.io client side.

The purpose of this is to manage VPS from the web with real time output on the commands and tasks that users set. The nodejs server will run on the VPS sever and spin off child processes to run each command. If it is possible, then i see it getting harder as each client should only be able to see the results of the commands they run and so the incoming php socket and the outgoing eventsource will some how need to be linked?

Any ideas? Cheers.

You can just run child_process.exec() and pipe the process stdout and stderr to the http response - there's no more to it than that.

The only hitch I'd see is if you want to let a user run whatever command they want, the Node app won't necessarily know MIME type to tell the browser that the response is. And at the time you need to send an HTTP response code, you don't necessarily know whether the process has succeeded or not - so if you actually want to use a 500 Internal Server Error if the remote process exits non-zero, you'll need to buffer output until the process exits, or wait a while and assume that if it hasn't failed after N seconds it's not going to.

I assume you realize that letting a process remotely run ad-hoc programs on a remote machine by just making an http request is exceedingly dangerous for reasons as numerous as there are programs that can be run.

Here's an example of how to take requests from the web, pipe them through a program and back out into the response. It takes an incoming image file, uses netpbm and potrace to convert it to SVG and send the result back to the client without ever writing it to disk - unix pipes are a wonderful thing. You'll need Linux or similar, w/ netpbm and potrace installed to actually use it: https://timboudreau.com/code/traceservice/file/tip/traceservice.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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