javascript - Nodejs telnet登录无法输入密码

标签 javascript node.js telnet

如果我从 Windows 命令行 telnet 就没有问题:

> telnet telehack.com
(... wait until a '.' appears)
. login
username? example
password? (examplepass) * * * * * * * 

Logged...
(...)
@ (This is the prompt when you are logged)

因此,使用 shell,我可以登录并执行所有操作,并且使用为 Nodejs 编写的 javascript 代码,我可以执行除输入密码之外的所有操作。然后我能够:连接到主机+端口,输入“登录”命令,输入“example”用户名(实际上存在),当我尝试输入“examplepass”(实际上也存在)时,telnet服务器会回答我'* * * * * * *' 但我无法跳过这一步。我将提供代码,感谢任何帮助。

var net = require('net');
var conn = net.createConnection(23, 'telehack.com');

conn.setEncoding('utf-8');

conn.on('connect', function() {  
  console.log('connected to the server');

  conn.on('data', function(data) {
    console.log('' + data);
    if (data.indexOf('username') != -1) {
      conn.write('example\r\n');
    }
    if (data.indexOf('pass') != -1) {
      conn.write('examplepass\r\n');
    }
    if (data.indexOf("@") != -1) {
      conn.write('date\r\n');
    }
    if (data.slice(-1) == '.'){
      console.log('slice: '+data.slice(-1));
      //conn.emit('login');
      conn.write('login\r\n');

    }
  });
});

最佳答案

Check this out.

var net = require('net');
var client = net.connect(parseInt(process.argv[3]),process.argv[2],function(){
    client.setEncoding('utf8');
    console.log('Connected!!');
    client.on('data',function(chunk){
        console.log(chunk);
    });
    process.stdin.resume(); // Activate STDIN
    process.stdin.setEncoding('utf8'); // Set it to string encoding
    process.stdin.on('data',function(chunk){ // Pipe STDIN to Socket.out
        client.write(chunk);
    });
});

您必须重新启用标准输入并将所有数据从标准输入发送到套接字。

关于javascript - Nodejs telnet登录无法输入密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22477153/

相关文章:

javascript - Electron 应用程序。多个html文件

node.js - Mongoose .save 不更新数据库

linux - 如何从开发箱检查某个端口是否打开或在任何其他服务器上被阻止?

javascript - 隐藏谷歌丰富网页摘要

javascript - 如何从 NodeJs 启动和停止 Electron 应用程序

javascript - 在 Javascript 中精确替换字符串

node.js - require 中的 Node js : Why module. Exports = tesdf() 仅在 get 请求中执行一次

Java 和 Telnet 响应不可读

networking - 是否可以通过 telnet 执行 SSL 握手?

javascript - 单击后保留边框/阴影