在 NodeJS 中连接到 telnet 服务器 [英] Connecting to a telnet server in NodeJS

查看:69
本文介绍了在 NodeJS 中连接到 telnet 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个特定的设备,它只能通过 telnet 进行控制,我已经这样做了一段时间(通过 PuTTy 或终端).

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.

在 NodeJS 中,我创建了一个 Web 界面,可以更轻松地控制设备.我通过为通过 telnet 发送的不同命令运行不同的 bash 脚本来做到这一点.这工作正常,但感觉太无组织了,更不用说每个命令的 shell 脚本的混乱了.我正在寻找一种更好的方法来做到这一点.

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.

我想到了直接从 NodeJS 连接到 telnet 服务器.我尝试了不同的软件包(即 net、telnet-client、telnet-stream、another-telnet-client 等),但要么这些不是我需要的工具,要么我只是没有正确实现它.

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.

这是我正在使用 telnet-client 包测试的示例代码(其他只是这个的变体):

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.

此代码不正确吗?我不熟悉 telnet,非常感谢您的帮助.

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

这篇关于在 NodeJS 中连接到 telnet 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆