2017-10-14 144 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系統。 –