Search code examples
javascriptelectrontelnet

How to send multiple pieces of data on same telnet connection?


An Electron App which uses ipcMain and ipcRenderer process to send telnet commands on click of a button along with npm telnet-client library for Telnet connection between desktop App & remote device (server). I'm new to promises so could not figure it out the right implementation.

Issue: every time a button is clicked a new Telnet session is established and my remote device becomes unresponsive after twenty concurrent sessions. To overcome this I've added connection.end() after connection.exec() completes on Telnet connection which somehow improves functionality by closing connection after sending command but also adds latency. However I also realized this does not work all the time and skips intermittently leaving the connection open.

Question: how can I check whether the Telnet session is already established and then send commands through the existing session instead creating new session every time and running behind closing it explicitly.

server.html

<script>
    const {ipcRenderer} = require('electron')
    function sendCommand(channel, arg){
        ipcRenderer.send(channel, arg)
    }
</script>

<button onclick="sendCommand('channel-1', 'command1');">  Command 1 </button>
<button onclick="sendCommand('channel-1', 'command2');">  Command 2 </button>

main.js

const {ipcMain} = require('electron')
var Telnet = require('telnet-client')
var connection = new Telnet()

ipcMain.on('channel-1', (event, arg) => {
  var params = {
    host: '192.168.1.121',
    port: 23,
    shellPrompt: ' ',
    timeout: 1500,
  }

var cmd = arg

  connection.on('ready', function(prompt) {
    connection.exec(cmd, function(err, response) {
    console.log(response)
    })
  })
  connection.on('timeout', function() {
    console.log('socket timeout!')
    connection.end()
  })
  connection.on('close', function() {
    console.log('connection closed')
  })
  connection.connect(params)
})

Thanks in advance.


Solution

  • The following modification helped me sending commands over the existing telnet channel without creating multiple sessions of telnet, now the speed of sending telnet command is fast.

    var params = {
      host: '192.168.1.121',
      port: 23,
      shellPrompt: ' ',
      timeout: 1500,
      // removeEcho: 4
    }
    connection.connect(params)
    
    var cmd = "a temp command"
    
    connection.on('ready', function(prompt) {
      connection.exec(cmd, function(err, response) {
        console.log(response)
      })
    })
    
    connection.on('timeout', function() {
      console.log('socket timeout!')
      connection.end()
    })
    
    connection.on('close', function() {
      console.log('connection closed')
    })
    
    ipcMain.on('channel-1', (event, arg) => {
    
      cmd = arg
    
    if(connection.getSocket().writable){
      connection.exec(cmd, function(err, response) {
          console.log(response)
      })
    }else{
      console.log("connection closed!" + connection.getSocket().writable)
    }
    
    })