I am working with node.js, and I am trying to embed a console in the web browser to work in a remote server. The web application do the connection so the user does not need to do the ssh username@host
but only type commands.
I have tried the node.js' ssh2
module and other modules which use ssh2. But I'm experiencing always the same problem. Every time I execute a command programmatically using exec()
, the ssh session is restarted. I'll explain it better with an example.
> ls
returns the content of home directory, one of the directories in the home directory is mydir
> cd mydir
> ls
returns the content of my home directory again, because after a command is executed the ssh session is closed/restarted.
Any node.js library which can do the job? or even a library of other technology different to javascript?
Edit: Other example for clarifying, using the node.js' module ssh-exec
The server has to execute some commands in other machine using ssh. A function in the server contains the following code
var c = exec.connection('username@host.com'); // It takes the ssh key from the default location
exec('cd mydir', c).pipe(process.stdout);
exec('ls -lh', c).pipe(process.stdout);
As you can see I am not ending the connection after the first exec
but the output I obtain is the content of the home directory not the content of mydir directory, because the ssh session is reset after each exec
.
The maintainer of node.js' ssh2 module provided the solution.
To use the method
shell()
instead of the methodexec()
.The method
shell()
creates an interactive session with the server we are connecting. The methodshell()
provides a stream as a parameter of its callback (like the methodexec()
).Like when using
exec()
,stream.on('data', function(data, extended) {...});
can be used to get the output of the commands. However, in this case, to provide commands (input) to the machine you connected with, you need to usestream.write(yourcommand+'\n');
PS. Feel free to edit to improve the accuracy of the answer.
I have to guess a bit, but you do something like
child = exec('ssh username@host ls')
?You can do something like
upfront and in the "loop" of your browser
When finished, just close
stdin
:which also finishes the child process.
I know this link is old but I figured this may help someone if they're looking for a solution. The
Works. Here's another solution. Use absolute file paths i.e.