简体   繁体   中英

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.

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. 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.

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. If you are on Windows(assumed here), the you'll need both \\r\\n .

I had similar issues when I tried to communicate to a telnet server through NodeJS. What solved my problem was to use an carriage return before the line feed character:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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