javascript - Nodejs ssh2 run multiple command only one terminal -
I am having a problem while coding the ssh2 module in my project. I tried to run a remote Linux system to run several commands on one terminal. For example "BC" command gives you the original calculator and you can run it for basic tasks. But you need to be aware that when you are using that kind of processes (it will accept two or more inputs and respond as a result).
I need to create a system like working with websocket ssh. When a websocket received an order, the SSID node needs to execute this message and the module needs to send it feedback via websocket.send ()
I node.js websocket , Using ssh2 client
My code here:
#! Requires / usr / bin / node var connection = ('ssh2'); Var conn = new connection (); Var command = ""; Var http = Required ('http'); Var requires WebSocketServer = ('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 ()) "listening on the server port:" + WebSocket serverport);}); //console.log ((new date ()) + 'server created'); WsServer = New WebSocketServer ({httpServer: server, // autoAcceptConnections: false}); WsServer.on ('request', function (request) {console.log ((new date ()) 'connection to' original '+ request.origin +'. '); Var wsconnection = request.accept (' echo-protocol ', Request.origin); console.log ((new date ()) +' connection accepted. '); If (! Ssh2ConnectionControl) {conn.connect ({host:' localhost ', port: 22, user name : 'Attilaakinci', password: '1'}); ssh2ConnectionControl = true; console.log ('connect SSH');} 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; // } Other {Conn.exec (message.utf8Data, onShell);}} wsconnect Ion.send (message.utf8Data);} else {console.log ('invalid message');}}); wsconnection.on ('close', function (Reasoncode, description) {console.log ((new date () ) + 'Peer' + wsconnection.remoteAddress + 'disconnected.');});});
You use conn.shell () instead conn.exec () If you want a real interactive shell, conn.exec () is usually to execute a one-liner command, so it Conn.exec () does not issue "shell state" between calls (like work directories, etc.).). You should be aware of potential limitations by your SSH server, as far as possible how many simultaneous shell / request requests are allowed per connection, I think that the default limit is 10 on the OpenSSH server.
Comments
Post a Comment