4

I'm attempting to make an interactive ssh session where I can send bash cmds and get back the result.

The following works:

var SSH2Promise = require('ssh2-promise');

var sshconfig = {
  host: '192.168.1.142',
  username: 'root',
  password: 'pass'
}
var ssh = new SSH2Promise(sshconfig);

ssh.connect().then(() => {console.log("Connection established")})

But what I really want to do is something like:

var ssh_socket
ssh.shell().then((socket) => {
  ssh_socket = socket
  socket.on('data', (data) => {
  console.log("ssh data: " + data)
})
socket.write("echo ssh shell connected\n")
})

ssh_socket.write("ls\n")

My call to ssh.shell shows nothing but I expected to see

"ssh data: ssh shell connected"

in the console. Furthermore, after this initialization, I hope to be able to send in many commands, a la

ssh_socket.write("ls\n")

and see their results.

user1343035
  • 245
  • 3
  • 13
  • I'm also looking for building an interactive shell to connect my web application to LXD container through SSH. Did you manage to code something that works ? – Yanover Apr 16 '21 at 08:50

0 Answers0