Connecting to a telnet server in NodeJS
Asked Answered
L

2

5

There is a particular equipment here that can only be controlled via telnet, which I have been doing (through PuTTy or Terminal) for a while now.

In NodeJS, I created a web interface that would make it easier to control the equipment. I did this by running different bash scripts for different commands sent through telnet. This is working fine, but it felt too unorganized, and not to mention the clutter of shell scripts for each command. I'm looking for a better way of doing this.

I thought of connecting to the telnet server directly from NodeJS. I have tried different packages (i.e. net, telnet-client, telnet-stream, another-telnet-client, etc) but it's either those aren't the tools I need or I'm just not implementing it correctly.

Here's a sample code I'm testing using the telnet-client package (others are just variations of this):

const telnet = require('telnet-client');
const server = new telnet();

// display server response
server.on("data", function(data){
    console.log(''+data);
});

// login when connected
server.on("connect", function(){
    server.write("login <user> <pass>");
});

// connect to server
server.connect({
    host: "172.16.0.1",
    port: 9600
});

When done manually, after the "login" request, I should see a "login successful" message but I'm not getting anything back.

Is this code incorrect? I'm not familiar with telnet and would really appreciate any help.

Leer answered 6/11, 2017 at 14:53 Comment(5)
By looking at the telnet-client library, I suspect that it's built for unix shell logins. Also, I could not find server.write method in that package. If, as it seems, you are communicating over a custom protocol, you should look into just opening a tcp socket on that address. Here's an example.Misfile
From the documentation of that package it seems you should wait for the server ready event before writing a login line. Take a look at the package example github.com/mkozjak/node-telnet-clientMoorehead
Hi niklon! It worked! Thanks so much! Apparently, adding "\r\n" at the end of the write function is necessary too as per Sami Hult's original advise. Thank you, guys!Leer
Hello, could you please explain how you were able to connect to the telnet-client server and initiate a log in? I copied there sample code from the npm telnet-client site, but every time I rune command node server.js (the file I have the code in), I get a err connection refused error. How do you connect to telnet-client and start a chat session?Yale
I ended up just using the net module instead of telnet-client. I found it much easier to use. I'm not sure about the connection refused error, though. Make sure you're pointing at the correct IP and port.Leer
L
11

Just going to write the answer here in case anyone's having the same problem.

My problem was fixed simply by adding a carriage return and a line feed at the end of the string I'm sending to the server.

server.write("login <user> <pass>\r\n");
Leer answered 17/11, 2017 at 21:56 Comment(0)
H
2

Try this code,just replace IP address and Port Number of your telnet server

var TelnetSocket, net, socket, tSocket;

net = require("net");

({TelnetSocket} = require("telnet-stream"));


socket = net.createConnection(23, "127.0.0.1");


tSocket = new TelnetSocket(socket);


tSocket.on("close", function() {
  return process.exit();
});


tSocket.on("data", function(buffer) {
  return process.stdout.write(buffer.toString("utf8"));
});


tSocket.on("do", function(option) {
  return tSocket.writeWont(option);
});


tSocket.on("will", function(option) {
  return tSocket.writeDont(option);
});


process.stdin.on("data", function(buffer) {
  return tSocket.write(buffer.toString("utf8"));
});


Hive answered 7/5, 2020 at 4:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.