javascript - Nodejs ssh2仅在一个终端运行多个命令

标签 javascript node.js shell ssh websocket

我在项目中编写 ssh2 模块时遇到了麻烦。我尝试在一个终端上运行多个命令来统治远程 Linux 系统。例如“bc”命令为您提供了一个基本的计算器,您可以运行它进行基本操作。但这种进程在使用时需要处于唤醒状态(它将接受两个或多个输入,并给出响应结果)。

我需要创建一个类似于 websocket 和 ssh 的系统。当 websocket 收到命令时,ssh Node 需要执行该消息,模块需要通过 websocket.send() 发送其响应

我正在使用 Node.js websocket、ssh2 客户端。

这是我的代码:

#!/usr/bin/node
var Connection = require('ssh2');
var conn = new Connection();

var command="";
var http = require('http');
var WebSocketServer = require('websocket').server;
var firstcom=true;
conn.on('ready', function() {
                console.log('Connection :: ready');
               // conn.shell(onShell);

            }); 

var onShell = function(err, stream) { 

                  //  stream.write(command+'\n');

                    stream.on('data', function(data) {
                        console.log('STDOUT: ' + data);
                    });

                    stream.stderr.on('data', function(data) {
                    console.log('STDERR: ' + data);

                   });

          }



var webSocketsServerPort=5000;
var ssh2ConnectionControl=false;

var server = http.createServer(function (req, res) {
  //blahbalh
}).listen(webSocketsServerPort, function() {
    console.log((new Date()) + " Server is listening on port:: " + webSocketsServerPort);
});

//console.log((new Date()) + 'server created');

wsServer = new WebSocketServer({
    httpServer: server,
 //   autoAcceptConnections: false
});

wsServer.on('request', function(request) {
    console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
    var wsconnection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');

    if(!ssh2ConnectionControl){
      conn.connect({
                host: 'localhost',
                port: 22,
                username: 'attilaakinci',
                password: '1'
              });
      ssh2ConnectionControl=true;
        console.log('SSH Connected.');       
    } 

    wsconnection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            command=message.utf8Data;

            //if(firstcom){
            //  conn.shell(onShell);
            //  firstcom=false;
            //}else{
              conn.exec(message.utf8Data,onShell);

            //}

            wsconnection.send(message.utf8Data);
        }
        else{
            console.log('Invalid message');
        }
    });

    wsconnection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + wsconnection.remoteAddress + ' disconnected.');
    });
});

最佳答案

如果您想要一个真正的交互式 shell,您应该使用 conn.shell() 而不是 conn.exec()。 conn.exec() 通常用于执行单行命令,因此它不会在 conn.exec() 调用之间保留“shell 状态”(例如工作目录等) .).

您还应该了解 SSH 服务器设置的可能限制,即每个连接允许同时执行的 shell/exec 请求数。我认为 OpenSSH 服务器上的默认限制是 10。

关于javascript - Nodejs ssh2仅在一个终端运行多个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24956159/

相关文章:

javascript - map 功能: anonymous function vs function expression

javascript - 如何模糊除列表项之外的整个主体?

node.js - 获取 Express 中的推荐值?

shell - Unix shell 标志

c - 我自己的 C Shell 的历史问题

bash - Colorized grep——查看整个文件并突出显示匹配项

javascript - Meteor admin 显示其他包的集合

javascript - JQuery 过滤函数忽略 html 元素并只搜索文本

javascript - 从 Facebook 群组中选择最喜欢的帖子

javascript - 为什么我的 socket.io 应用程序在 Raspberry pi 上运行时会立即断开连接?