The below code is a modified version from the one used for PHP on this link: http://www.scriptol.com/javascript/nodejs-php.php
This functionality could be useful in doing many automation tasks for Ops, though could be done by other tools like Puppet or even Jenkins but NodeJs is very light weight and quite fast.
Would plan to integrate this in my HTML status pages in the future.
The code:
var sys = require("sys"),
http = require("http"),
path = require("path"),
url = require("url"),
filesys = require("fs"),
runner = require("child_process");
function sendError(errCode, errString, response)
{
response.writeHead(errCode, {"Content-Type": "text/plain;charset=utf-8"});
response.write(errString + "\n");
response.end();
return false;
}
function sendData(err, stdout, stderr, response)
{
if (err) return sendError(500, stderr, response);
response.writeHead(200,{"Content-Type": "text/plain;charset=utf-8"});
response.write(stdout);
response.end();
}
function runScript(exists, file, response)
{
if(!exists) return sendError(404, 'File not found', response);
runner.exec(file ,
function(err, stdout, stderr) { sendData(err, stdout, stderr, response); });
}
function myshell(request, response)
{
var urlpath = url.parse(request.url).pathname;
//var param = url.parse(request.url).query;
var localpath = path.join("/", urlpath);
console.log(localpath);
filesys.exists(localpath, function(result) { runScript(result, localpath, response)});
}
var server = http.createServer(myshell);
server.listen(7071);
console.log("Shell ready to run script given on port 7071.");
HTML:
<html>
<head>
<meta http-equiv="refresh" content="600" />
</head>
<body>
<br>
<a href=http://localhost:7071/bin/ls>Run ls Locally</a>
<br>
<br>
</body>
</html>
This functionality could be useful in doing many automation tasks for Ops, though could be done by other tools like Puppet or even Jenkins but NodeJs is very light weight and quite fast.
Would plan to integrate this in my HTML status pages in the future.
The code:
var sys = require("sys"),
http = require("http"),
path = require("path"),
url = require("url"),
filesys = require("fs"),
runner = require("child_process");
function sendError(errCode, errString, response)
{
response.writeHead(errCode, {"Content-Type": "text/plain;charset=utf-8"});
response.write(errString + "\n");
response.end();
return false;
}
function sendData(err, stdout, stderr, response)
{
if (err) return sendError(500, stderr, response);
response.writeHead(200,{"Content-Type": "text/plain;charset=utf-8"});
response.write(stdout);
response.end();
}
function runScript(exists, file, response)
{
if(!exists) return sendError(404, 'File not found', response);
runner.exec(file ,
function(err, stdout, stderr) { sendData(err, stdout, stderr, response); });
}
function myshell(request, response)
{
var urlpath = url.parse(request.url).pathname;
//var param = url.parse(request.url).query;
var localpath = path.join("/", urlpath);
console.log(localpath);
filesys.exists(localpath, function(result) { runScript(result, localpath, response)});
}
var server = http.createServer(myshell);
server.listen(7071);
console.log("Shell ready to run script given on port 7071.");
HTML:
<html>
<head>
<meta http-equiv="refresh" content="600" />
</head>
<body>
<br>
<a href=http://localhost:7071/bin/ls>Run ls Locally</a>
<br>
<br>
</body>
</html>
No comments:
Post a Comment