var express = require('express'); var router = express.Router(); var fs = require('fs'); var template_render = require('../lib/render_template.js'); var authorize = require('../lib/authorize.js'); router.get('/', authorize.auth, function(req, res, next) { var content = ""; content = template_render.get_template("dhcp_start_stop_restart"); content = template_render.set_template_variable(content, "title", "Start / Stop / Restart"); var exec = require('child_process').exec; exec(' ps aux | grep dhcpd | grep -v "grep"', function(err, stdout, stderr) { is_running = 0; var result = stdout.split("\n"); for (var i = 0; i < result.length; i++) { if(/dhcpd/i.test(result[i])){ is_running = 1; } // console.log('line ' + result[i]); } var return_content = ""; if(is_running){ return_content = return_content + 'DHCP Server is online!

'; return_content = return_content + ' '; } else { return_content = return_content + 'DHCP Server is offline!

'; return_content = return_content + ' '; } return_content = return_content + ' '; content = template_render.set_template_variable(content, "c_content", return_content); res.send(template_render.get_index_template(content, req.url)); }); }); router.post('/', authorize.auth, function(req, res, next) { var request = req.body; const execSync = require('child_process').execSync; switch (request.action) { case "stop": dhcp_exec = execSync('service isc-dhcp-server stop && sleep 1'); res.send(""); break; case "start": dhcp_exec = execSync('service isc-dhcp-server start'); res.send(""); break; case "restart": dhcp_exec = execSync('service isc-dhcp-server restart && sleep 1'); res.send(""); break; default: break; } console.log(request); }); module.exports = router;