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.
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");