-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverfixed.js
More file actions
65 lines (52 loc) · 1.48 KB
/
serverfixed.js
File metadata and controls
65 lines (52 loc) · 1.48 KB
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
'use strict';
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import { spawn } from 'child_process';
import { execFile } from 'child_process';
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, './'));
var output = '';
var pingoutput = '';
app.get('/', (req, res) => {
res.render('index', {
output: null,
pingoutput: null,
});
});
app.post('/', (req, res) => {
const ping = req.body.ping;
const ping1 = req.body.ping1;
if (ping) {
let childProcess = spawn('/bin/ping', ['-c', '3', ping]);
childProcess.stdout.setEncoding('utf8');
childProcess.stdout.on('data', function (data) {
data = data.toString();
output += data;
});
childProcess.stderr.setEncoding('utf8');
childProcess.stderr.on('data', function (data) {
data = data.toString();
output += data;
});
childProcess.on('close', function (code) {
console.log('closing code: ' + code);
res.render('index', { output: output, pingoutput: null });
});
}
if (ping1) {
execFile('/bin/ping', ['-c', '3', ping1], function (err, stdout, stderr) {
pingoutput = stdout + stderr;
res.render('index', {
pingoutput: pingoutput,
output: null,
});
});
}
});
// ping directory: /usr/bin/ping or /usr/bin/ping
app.listen(3000, () => console.log('Listening on Port:3000'));