2017-10-14 147 views
2

我的要求是从节点js连接到Telnet客户端。套接字超时! Telnet连接使用节点telnet-client问题

我使用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)` 

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

我也params中

添加“用户名”和“密码”的详细信息
`var params = { 
     host: '127.0.0.1', 
     port: 23, 
     shellPrompt: '/ #', 
     loginPrompt: 'Username: ', 
     passwordPrompt: 'Password: ', 
     username: 'vinit', 
     password: 'vinit123', 
     initialLFCR: true, 
     timeout: 1500, 
     // removeEcho: 4 
}` 

,但仍然面临着同样的问题,试过。在一些链接中,我发现有人说shellPrompt值不正确,那么应该是什么值shellPrompt。实际上,我对这个话题是全新的,所以对此没有太多的想法。

任何帮助将不胜感激。提前致谢。

+0

您是否尝试更改超时值? – hasbi

+0

@hasbi是的,我试图改变它到10000,但仍然显示相同的结果。 –

+0

你能让它成为-1吗? – hasbi

回答

2

根据您的意见,这不是一个网络连接问题,这意味着您的连接在登录过程中失败,或者之后无法检测到有效提示(然后等待它超时)。

所以,你应该做的第一件事,就是添加failedLogin回调:

connection.on('failedlogin',function(msg) { 
    console.log("Login failed !",msg); 
}); 

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

接下来,“手动”连接到您的服务器,即通过从命令行运行telnet 127.0.0.1,并检查“login/password”和“shell”提示的外观。

E.g.我的 “的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 

[email protected]:~$ 
  • 登录提示是zeppelin-desktop login:
  • 密码提示是Password:
  • 的shell提示符是[email protected]:~$

登录名和密码提示匹配其预定义模式telnet-client

loginPrompt: Username/login prompt that the host is using. Can be a string or an instance of RegExp. Defaults to regex '/login[: ]*$/i'.

passwordPrompt: Password/login prompt that the host is using. Can be a string or an instance of RegExp. Defaults to regex '/Password: /i'.

所以没有必要改变它们。

Shell提示符不匹配的预定图案(/ #):

shellPrompt: Shell prompt that the host is using. Can be a string or an instance of RegExp. Defaults to regex '/(?:/)?#\s/'.

因此它需要被修饰,例如

shellPrompt: /:~[^$]*\$\s*$/, 

您可以测试你的shell提示符模式使用grep,这样的:

%echo '[email protected]:~$' | grep -o ':~[^$]*\$\s*$' 
:~$ 

(输出将是空的,如果它不匹配)

现在,你有一个正确的壳提示模式,telnet-client应该能够识别它,并将cmd(在on('ready')处理程序中)发送到服务器,以响应它。

E.g.如果设置为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 

P.S.

你可能要设置的另一个选项是“登录失败”的格局:

failedLoginMatch : "Login incorrect" 

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

这不是我的Ubuntu系统上绝对必要的,因为它只会 重复的失败密码的情况下登录提示,使failedlogin事件无论如何都会被派出,但根据您的配置, 它可能是必需的。

+0

虽然。我已经自己解决了它。但这是正确的答案。感谢您发布它。它会帮助别人 –

0

这是一个连接超时:在目标IP:端口上没有运行Telnet服务器,或该端口在目标防火墙中被阻止。

+0

我可以从终端连接到相同。所以,凭证是正确的。 –

+0

你对我在某些地方发现的'shellPrompt'的价值有任何想法,可能是因为shell的价值提示我不正确。在那种情况下,我怎么能找到我的Ubuntu系统。 –