-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (62 loc) · 1.85 KB
/
Copy pathserver.js
File metadata and controls
71 lines (62 loc) · 1.85 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
66
67
68
69
70
71
const express = require("express");
const fs = require("fs");
const http = require("http");
var SSHClient = require("ssh2").Client;
var utf8 = require("utf8");
const app = express();
var serverPort = 8080;
var server = http.createServer(app);
//set the template engine ejs
app.set("view engine", "ejs");
//middlewares
app.use(express.static("public"));
//routes
app.get("/", (req, res) => {
res.render("index");
});
server.listen(serverPort);
//socket.io instantiation
const io = require("socket.io")(server);
//Socket Connection
io.on("connection", function(socket) {
var ssh = new SSHClient();
ssh
.on("ready", function() {
socket.emit("data", "\r\n*** SSH CONNECTION ESTABLISHED ***\r\n");
connected = true;
ssh.shell(function(err, stream) {
if (err)
return socket.emit(
"data",
"\r\n*** SSH SHELL ERROR: " + err.message + " ***\r\n"
);
socket.on("data", function(data) {
stream.write(data);
});
stream
.on("data", function(d) {
socket.emit("data", utf8.decode(d.toString("binary")));
})
.on("close", function() {
ssh.end();
});
});
})
.on("close", function() {
socket.emit("data", "\r\n*** SSH CONNECTION CLOSED ***\r\n");
})
.on("error", function(err) {
console.log(err);
socket.emit(
"data",
"\r\n*** SSH CONNECTION ERROR: " + err.message + " ***\r\n"
);
})
.connect({
host: "YOUR_HOST",
port: "PORT", // Generally 22 but some server have diffrent port for security Reson
username: "USER", // user name
password: "PASSWORD" // Set password or use PrivateKey
// privateKey: require("fs").readFileSync("PATH OF KEY ") // <---- Uncomment this if you want to use privateKey ( Example : AWS )
});
});