Skip to content Skip to sidebar Skip to footer

Ssh Persistent Sessions With Node.js

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 t

Solution 1:

The maintainer of node.js' ssh2 module provided the solution.

To use the method shell() instead of the method exec().

The method shell() creates an interactive session with the server we are connecting. The method shell() provides a stream as a parameter of its callback (like the method exec()).

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 use stream.write(yourcommand+'\n');

PS. Feel free to edit to improve the accuracy of the answer.

Solution 2:

I have to guess a bit, but you do something like child = exec('ssh username@host ls')?

You can do something like

child = exec('ssh username@host');

upfront and in the "loop" of your browser

child.stdin.write('ls\n');

When finished, just close stdin:

child.stdin.end()

which also finishes the child process.

Solution 3:

I know this link is old but I figured this may help someone if they're looking for a solution. The

To use the method shell() instead of the method exec().

Works. Here's another solution. Use absolute file paths i.e.

conn.exec("mkdir -p /home/user/Direc/{one,two,three}/", function(err, stream) {
        if (err) throw err;
        stream.on('data', function(data) {
            console.log('STDOUT: ' + data);
        }).stderr.on('data', function(data) {
            console.log('STDERR: ' + data);
        });
    });

conn.exec("ls -la /home/user/", function(err, stream) {
    if (err) throw err;
    stream.on('close', function(code, signal) {
        console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
        conn.end();
    }).on('data', function(data) {
        console.log('STDOUT: ' + data);
    }).stderr.on('data', function(data) {
        console.log('STDERR: ' + data);
    });
});

Post a Comment for "Ssh Persistent Sessions With Node.js"