blessed
blessed copied to clipboard
Blank when using over SSH
I'm attempting to create a SSH2 server based off https://github.com/mscdex/ssh2 When setting input and output on the screen as the 'accept()' of SSH2's shell event, which is an instance of Stream with type Duplex and has stdin and stdout. Reading input from the stream works correctly as i can destroy the screen and close the socket when pressing Q on a screen.key listener. however the screen is blank on the client's side.. It does do something, making the screen blank completely, but executing screen.render() doesn't seem to do anything.. If I disable the output property on the screen object it is correctly displaying on the server end. This indicates to me that the code does actually work, just not over the SSH2 connection.
Gist of a test case for this.
https://gist.github.com/AviiNL/4b920432024741f1e80ff9144db4f5c5
Hey @AviiNL I too am interested in using blessed with ssh2 so after some digging I noticed that there was a single character being displayed on the ssh client, in the top left corner. Then realized that the screen rows and columns was set to 1. I tried to set screen.rows etc but it never took effect and returned 1 right after setting. From the telnet example found https://github.com/chjj/node-telnet2 & https://github.com/chjj/blessed#server-side-usage I saw that the rows and columns was being set on the stream and then 'resize' was being emitted. After doing that things started working.
The 'window-change' event didn't seem to get called initially, only when things are actually resized, not to worry. So we need to resize on window change and set things one time initially. I've put together a working script, hope this helps.
const blessed = require('blessed');
const fs = require('fs');
const ssh2 = require('ssh2');
ssh2.Server({
hostKeys: [
fs.readFileSync('host.key')
]
}, (client) => {
client.on('authentication', (ctx) => {
ctx.accept();
}).on('session', (accept, reject) => {
let screen, info, stream, session = accept();
let windowChange = () => {
if(stream) {
stream.rows = info.rows;
stream.columns = info.cols;
stream.emit('resize');
}
};
session.on('pty', (accept, reject, info_) => {
accept();
info = info_;
});
session.on('window-change', (accept, reject, info_) => {
info.rows = info_.rows;
info.cols = info_.cols;
windowChange();
});
session.on('shell', (accept, reject) => {
stream = accept();
screen = blessed.screen({
smartCSR: true,
terminal: 'xterm-256color',
input: stream,
output: stream,
});
windowChange();
screen.on('destroy', () => {
stream.exit(0);
stream.end();
});
myScreen(screen);
});
client.on('close', () => {
if(screen && !screen.destroyed) {
screen.destroy();
}
});
});
}).listen(12345, 'localhost', function() {
let addr = this.address();
console.log(`Listening... ${addr.address} port ${addr.port}`);
});
function myScreen(screen) {
screen.key(['C-c', 'q'], (ch, key) => {
screen.destroy();
});
blessed.box({
parent: screen,
top: 0,
left: 0,
width: '100%',
height: '100%',
border: 'line',
content: 'Hello World!',
style: {
bg: 'red',
},
});
screen.render();
}
Hello,
Thanks to you, I successfully setup blessed
with ssh2
.
However, I also need to use screen.spawn
or screen.exec
, but these methods doesn't redirect the output to the SSH console but to the Node console.
How can I fix this ?
Thanks