在使用nodejs进行telnetlogin时不能input密码

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

> telnet telehack.com (... wait until a '.' appears) . login username? example password? (examplepass) * * * * * * * Logged... (...) @ (This is the prompt when you are logged) 

所以在shell中,我可以login并执行所有操作,并且使用为nodejs编写的JavaScript代码,我可以完成所有操作,但input密码。 然后,我可以:连接到主机+端口,input“login”命令,input“示例”用户名(实际存在),当我尝试input“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'); } }); }); 

看一下这个。

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

你必须重新启用stdin,并将任何数据从stdin发送到套接字。