Created
June 3, 2021 08:45
-
-
Save davit312/1f90014f5df9a929238127dae56a14e9 to your computer and use it in GitHub Desktop.
Execute commands on remote server with node js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http') | |
const qs = require('querystring') | |
const {exec} = require('child_process') | |
const PASSWORD = 'your secure password' | |
const PORT = 8080 | |
const PATH = '/' | |
let server = http.createServer(function(req,resp) { | |
if(req.method == 'GET'){ | |
resp.write(`<!DOCTYPE html> | |
<body> | |
<form action="${PATH}" method="POST" enctype="application/x-www-form-urlencoded"> | |
<input name="password" type="password" placeholder="password"> | |
<input name="command" type="text" placeholder="command"> | |
<input type="submit"> | |
</form> | |
</body>`) | |
resp.end() | |
} | |
if (req.method == 'POST') { | |
let body = ''; | |
req.on('data', function (data) { | |
body += data; | |
}); | |
req.on('end', function () { | |
let post = qs.parse(body) | |
if(post['password'] == PASSWORD){ | |
exec(post['command'], function(err, stdout, stderr){ | |
if(err){ | |
resp.write('ERROR: exit code '+err.code+'\n' + err.message) | |
} | |
else if(stderr){ | |
resp.write('STDERR: \n' + stderr) | |
} | |
if(stdout) { | |
resp.write('STDOUT: \n' + stdout) | |
} | |
resp.end() | |
}) | |
} | |
}); | |
} | |
}) | |
server.listen(PORT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment