套接字超时!使用节点telnet-client进行Telnet连接时出现问题

Vin*_*ngh 2 sockets node.js npm

我的要求是从节点js连接到Telnet客户端。

我正在使用telnet-client软件包

我正在使用此代码进行连接

var Telnet = require('telnet-client')
var connection = new Telnet()

var params = {
  host: '127.0.0.1',
  port: 23,
  shellPrompt: '/ # ',
  timeout: 1500,
  // removeEcho: 4
}

connection.on('ready', function(prompt) {
connection.exec(cmd, function(err, response) {
console.log(response)
  })
})

connection.on('timeout', function() {
console.log('socket timeout!')
connection.end()
})

connection.on('close', function() {
  console.log('connection closed')
})

connection.connect(params)`
Run Code Online (Sandbox Code Playgroud)

但是它总是返回“套接字超时!” 在控制台中。

我还尝试通过在参数中添加“用户名”和“密码”详细信息

`var params = {
        host: '127.0.0.1',
        port: 23,
        shellPrompt: '/ #',
        loginPrompt: 'Username: ',
        passwordPrompt: 'Password: ',
        username: 'vinit',
        password: 'vinit123',
        initialLFCR: true,
        timeout: 1500,
        // removeEcho: 4
}`
Run Code Online (Sandbox Code Playgroud)

but still facing the same issue. In some links i found people saying that shellPrompt value is incorrect then what should be the value of shellPrompt. actually i am completely new to this topic so don't have much idea about it.

Any help will be appreciated. Thanks in advance.

zep*_*lin 5

根据您的评论,这不是网络连接问题,这意味着您的连接要么在登录期间失败,要么之后无法检测到有效的提示(然后等待直到超时)。

因此,您应该做的第一件事就是添加failedLogin回调:

connection.on('failedlogin',function(msg) {
    console.log("Login failed !",msg);
});
Run Code Online (Sandbox Code Playgroud)

要检测“登录失败”的情况(否则,它将尝试登录一次,然后挂起直到超时)。

接下来,“手动”连接到服务器,即telnet 127.0.0.1从命令行运行,然后查看“登录/密码”和“外壳”提示的外观。

例如,在我的“ Ubuntu 16.04.3 LTS”系统上:

%telnet 127.0.0.1
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Ubuntu 16.04.3 LTS
zeppelin-desktop login: zeppelin
Password: 
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-97-generic x86_64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

zeppelin@zeppelin-desktop:~$
Run Code Online (Sandbox Code Playgroud)
  • 登录提示是 zeppelin-desktop login:
  • 密码提示是 Password:
  • shell提示是 zeppelin@zeppelin-desktop:~$

登录和密码提示确实与telnet-client 的预定义模式匹配

loginPrompt:主机正在使用的用户名/登录提示。可以是字符串或RegExp的实例。默认为正则表达式'/ login [:] * $ / i'。

passwordPrompt:主机正在使用的密码/登录提示。可以是字符串或RegExp的实例。默认为正则表达式'/ Password:/ i'。

因此,无需更改它们。

Shell提示与预定义的模式()不匹配/ #

shellPrompt:Shell提示主机正在使用。可以是字符串或RegExp的实例。默认为正则表达式'/(?:/)?#\ s /'。

因此需要对其进行修改,例如

shellPrompt: /:~[^$]*\$\s*$/,
Run Code Online (Sandbox Code Playgroud)

您可以使用grep测试shell提示模式,如下所示:

%echo 'zeppelin@zeppelin-desktop:~$' | grep -o  ':~[^$]*\$\s*$'
:~$
Run Code Online (Sandbox Code Playgroud)

(如果不匹配,则输出为空)

既然您具有正确的shell提示模式,则telnet-client应该能够识别它,并作为响应将其cmd(在on('ready')处理程序中)发送到服务器。

例如,如果您设置cmd="date",您应该得到当前日期作为响应:

connection.on('ready', function(prompt) {
    console.log(">date");
    connection.exec("date", function(err, response) {
       console.log("<",response)
    })
})

%nodejs telnet.js
>date
< Wed Oct 25 10:11:11 UTC 2017
Run Code Online (Sandbox Code Playgroud)

聚苯乙烯

您可能要设置的另一个选项是“登录失败”模式:

failedLoginMatch : "Login incorrect"
Run Code Online (Sandbox Code Playgroud)

检测服务器拒绝您的密码。

在我的Ubuntu系统上,这并不是绝对必要的,因为在密码输入失败的情况下,它只会重复登录提示,因此该failedlogin事件仍会被分派,但根据您的配置,可能是必需的。