Skip to content

Instantly share code, notes, and snippets.

@mscdex
Last active July 2, 2022 20:10
Show Gist options
  • Save mscdex/941f9c6af6733d18e6e7 to your computer and use it in GitHub Desktop.
Save mscdex/941f9c6af6733d18e6e7 to your computer and use it in GitHub Desktop.
ssh shell session from the browser

Instructions:

  1. npm install express faye-websocket ssh2 term.js
  2. Create a public subdirectory
  3. Place client.htm in public
  4. Edit the ssh connection details in server.js. See the documentation for the connect() method in the ssh2 readme for more information.
  5. Start the server with node server.js
  6. Visit http://localhost:8000/client.htm in a WebSocket-capable browser
<html>
<head>
<title>WebTerm</title>
<script src="/term.js"></script>
<script>
window.addEventListener('load', function() {
var Socket = window.MozWebSocket || window.WebSocket;
socket = new Socket('ws://' + location.hostname + ':' + location.port);
socket.onopen = function() {
var term = new Terminal({
cols: 250,
rows: 100,
convertEol: true,
useStyle: true,
cursorBlink: true,
screenKeys: true
});
term.on('data', function(data) {
socket.send(data);
});
term.on('title', function(title) {
document.title = title;
});
term.open(document.body);
socket.onmessage = function(event) {
term.write(event.data);
};
socket.onclose = function() {
term.destroy();
};
};
}, false);
</script>
</head>
<body>
</body>
</html>
var WebSocket = require('faye-websocket');
var express = require('express');
var app = express();
var server = require('http').Server(app);
var term = require('term.js');
var ssh = require('ssh2');
server.on('upgrade', function(request, socket, body) {
if (WebSocket.isWebSocket(request)) {
var ws = new WebSocket(request, socket, body);
var conn;
ws.on('open', function() {
conn = new ssh();
conn.on('ready', function() {
ws.write('\n*** SSH CONNECTION ESTABLISHED ***\n');
conn.shell(function(err, stream) {
if (err) {
ws.write('\n*** SSH SHELL ERROR: ' + err.message + ' ***\n');
conn.end();
return;
}
// Force binary strings to be sent over websocket to prevent
// getting a Blob on the browser side which term.js cannot
// currently handle
stream.setEncoding('binary');
stream.pipe(ws).pipe(stream).on('close', function() {
conn.end();
});
});
}).on('close', function() {
ws.write('\n*** SSH CONNECTION CLOSED ***\n');
ws.close();
}).connect({
host: '192.168.100.150',
port: 22,
username: 'foo',
password: 'bar'
});
}).on('close', function(event) {
try {
conn && conn.end();
} catch (ex) {}
});
}
});
server.listen(8000, '127.0.0.1');
app.use(express.static(__dirname + '/public'));
app.use(term.middleware());
@simplyjarod
Copy link

Two errors in server.js:

  1. Change var ssh = require('ssh2'); to var { Client } = require('ssh2');
  2. Change conn = new ssh(); to conn = new Client();

Everything else should work smoothly.
Many thanks!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment