Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

node 怎么调用shell,可以做一个web shell吗?

想做一个后台,页面里有个按钮,点这个按钮,就能执行规定好的shell脚本,然后能在页面打印控制台的信息!麻烦了解的告知需要那些东西或相关资料(自己用,简单点就可以了),谢谢了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
const EXEC = require('child_process').execSync;
const SPAWN = require('child_process').spawn;
const RL = require('readline');

使用这三个东东 + express,基本能能实现
比如:

var fnlist = ["1.bz2"];
var rl = RL.createInterface({
    input: SPAWN('bzcat', fnlist).stdout,
    output: process.stdout,
    terminal: false
});

然后你

rl.on('line', function(line){
    //我得到了shell的一行输出
}).on('close', function(){
    //shell完成啦
});

注意很多都是异步的,以及node进程本身的权限、安全问题


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...