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.
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. – Misfileready
event before writing a login line. Take a look at the package example github.com/mkozjak/node-telnet-client – Mooreheadwrite
function is necessary too as per Sami Hult's original advise. Thank you, guys! – Leernode 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? – Yalenet
module instead oftelnet-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