node-ssh icon indicating copy to clipboard operation
node-ssh copied to clipboard

Pseudo-TTY allocation?

Open codiophile opened this issue 1 year ago • 3 comments

For transparency, I have a service that starts long running commands over SSH, and I want it to be able to resume those SSH sessions after a restart. I thought I would be able to do it using screen/tmux, but they both complain about the lack of a terminal. I read about it, and when using the ssh command, the -t flag will force a pseudo-tty to be allocated, which enables you to run screen based programs.

Fortunately, I've found some evidence that this should be possible to do with SSH2: https://github.com/mscdex/ssh2/issues/718#issuecomment-406823236 https://github.com/mscdex/ssh2/issues/769#issuecomment-468910349

Unfortunately, I have no idea how that translates to node-ssh. It also looks fairly complicated and involves writing directly to streams and listening to events. Considering that node-ssh is a thin wrapper, I wouldn't be surprised if it exposes the necessary APIs, but you'd probably still have to write to streams and listen to events to make it work.

I think this issue should primarily be considered a feature request for a neat API to execute commands with a pseudo-tty. I would greatly appreciate if you could correct me, if any of my assumptions are wrong. I'm assuming that at the time of writing, I'm better off using SSH2 directly for accomplishing this. I'm fairly likely to attempt to implement this with SSH2 and wrap it in promises, which could possibly be incorporated in node-ssh.

codiophile avatar Nov 13 '24 14:11 codiophile

I seem to have figured out a way to do it. It's by no means a pretty solution, but basically, requestShell returns a stream that you can connect just like in the example here: https://github.com/mscdex/ssh2/issues/718#issuecomment-406823236

codiophile avatar Nov 13 '24 16:11 codiophile

I found it:

await ssh.execCommand('screen -r', { execOptions: { pty: true } })

Would have saved me a lot of time if there was a section about it in the documentation. :)

codiophile avatar Nov 13 '24 17:11 codiophile

Hi @codiophile! Thank you for posting your solution. Would you be interested in making a PR to the README to document this?

Happy New Year! 🌠

steelbrain avatar Jan 04 '25 06:01 steelbrain