简体   繁体   English

使用 JavaScript 进行 Telnet 连接

[英]Telnet connection with JavaScript

I am working with a Numato relay, and I try to connect to it via telnet using javascript with net.Socket, and then send commands.我正在使用 Numato 中继,我尝试使用带有 net.Socket 的 javascript 通过 telnet 连接到它,然后发送命令。

However, when trying to connect, it rejects the username and password.但是,当尝试连接时,它拒绝用户名和密码。 The problem is that strange characters are entered after entering the username.问题是输入用户名后输入了奇怪的字符。

In the code you can see that I tried to look at the outputs in various ways, with no results.在代码中,您可以看到我尝试以各种方式查看输出,但没有结果。

If anyone knows why or has worked with Numato modules I would appreciate any help.如果有人知道原因或使用过 Numato 模块,我将不胜感激。 Here is the code and its output:这是代码及其输出:

    var net = require('net');

    var client = new net.Socket();

    client.connect(23, '192.168.1.100', function() {
      console.log('Connected');
    });
    client.on('connect', function(){
      client.write('admin\n')
      client.write('admin\n')
      // client.write('admin\n' + 'admin\n')
    })

    client.on('data', function(data) {
      console.log(''+data)
      // var datos = []
      // datos.push(data)
      // datos.push(data.toJSON)
      // console.log(datos)
      // client.destroy(); // kill client after server's response
    });

    client.on('close', function() {
      console.log('Connection closed');
    });

输出图像

According to the Numato documentation this channel requires ASCII character encoding rather than the UTF encoded strings you are sending.根据Numato 文档,此通道需要 ASCII 字符编码,而不是您发送的 UTF 编码字符串。

Change:改变:

    client.on('connect', function(){
      client.write('admin\n')
      client.write('admin\n')
      // client.write('admin\n' + 'admin\n')
    })

To:到:

    client.on('connect', function(){
      client.write('admin\n', 'ascii')
      client.write('admin\n', 'ascii')
      // client.write('admin\n' + 'admin\n')
    })

NOTE: eol's answer is likely applicable here too.注意:eol 的回答可能也适用于此。 If you are on Windows(assumed here), the you'll need both \\r\\n .如果您使用的是 Windows(此处假设),则您将同时需要\\r\\n

I had similar issues when I tried to communicate to a telnet server through NodeJS.当我尝试通过 NodeJS 与 telnet 服务器通信时,我遇到了类似的问题。 What solved my problem was to use an carriage return before the line feed character:解决我的问题是在换行符之前使用回车符:

client.write('admin\r\n');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM