1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| const express = require("express"); const app = express(); const port = 3000; const PROJECT_DOMAIN = process.env.PROJECT_DOMAIN; var exec = require("child_process").exec; const os = require("os"); const { createProxyMiddleware } = require("http-proxy-middleware"); var request = require("request"); var fs = require("fs"); var path = require("path");
app.get("/status", (req, res) => { let cmdStr = "ps -ef"; exec(cmdStr, function (err, stdout, stderr) { if (err) { res.type("html").send("<pre>命令行执行错误:\n" + err + "</pre>"); } else { res.type("html").send("<pre>命令行执行结果:\n" + stdout + "</pre>"); } }); });
app.get("/listen", function (req, res) { let cmdStr = "ss -nltp"; exec(cmdStr, function (err, stdout, stderr) { if (err) { res.type("html").send("<pre>命令行执行错误:\n" + err + "</pre>"); } else { res.type("html").send("<pre>获取系统监听端口:\n" + stdout + "</pre>"); } }); });
app.get("/info", (req, res) => { let cmdStr = "cat /etc/*release | grep -E ^NAME"; exec(cmdStr, function (err, stdout, stderr) { if (err) { res.send("命令行执行错误:" + err); } else { res.send( "命令行执行结果:\n" + "Linux System:" + stdout + "\nRAM:" + os.totalmem() / 1000 / 1000 + "MB" ); } }); });
app.get("/test", (req, res) => { fs.writeFile("./test.txt", "这里是新创建的文件内容!", function (err) { if (err) res.send("创建文件失败,文件系统权限为只读:" + err); else res.send("创建文件成功,文件系统权限为非只读:"); }); });
app.use( "/" + "*", createProxyMiddleware({ target: "http://127.0.0.1:端口号/", changeOrigin: false, ws: true, logLevel: "error", onProxyReq: function onProxyReq(proxyReq, req, res) { } }) );
exec("启动指令", function (err, stdout, stderr) { if (err) { console.error(err); return; } console.log(stdout); });
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|