Reputation: 1363
My requirement is to connect to Telnet client from node js.
I am using telnet-client package
I am using this code for connection
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)`
But it always return "socket timeout!" in console.
I also tried by adding 'username' and 'password' details in 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
}`
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.
Upvotes: 4
Views: 5520
Reputation: 9365
Based on your comments, that is not a networking connectivity issue, which means that your connection fails either during the login, or it fails to detect the valid prompt afterwards (and then waits for it until it times out).
So the first thing you should do, is add the failedLogin
callback:
connection.on('failedlogin',function(msg) {
console.log("Login failed !",msg);
});
To detect the "failed login" case (otherwise it will attempt to login once, and then hang until the timeout comes).
Next, connect to your server "by hand", i.e. by running telnet 127.0.0.1
from the command line, and review what the "login/password" and "shell" prompts do look like.
E.g. on my "Ubuntu 16.04.3 LTS" system:
%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:~$
zeppelin-desktop login:
Password:
zeppelin@zeppelin-desktop:~$
Login and password prompts do match their predefined patterns in 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'.
So there is no need to change them.
Shell prompt does not match the the predefined pattern (/ #
):
shellPrompt: Shell prompt that the host is using. Can be a string or an instance of RegExp. Defaults to regex '/(?:/ )?#\s/'.
so it needs to be modified, e.g.
shellPrompt: /:~[^$]*\$\s*$/,
You can test your shell prompt pattern with grep, like that:
%echo 'zeppelin@zeppelin-desktop:~$' | grep -o ':~[^$]*\$\s*$'
:~$
(the output will be empty if it does not match)
Now that you have a correct shell prompt pattern, telnet-client
should be able to identify it, and send the cmd
(in the on('ready')
handler) to server, in response to it.
E.g. if you set cmd="date"
, you should get the current date in response:
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.
Another option you may want to set is the "failed login" pattern:
failedLoginMatch : "Login incorrect"
to detect the server rejecting your password.
This is not strictly necessary on my Ubuntu system, as it will just
repeat the login prompt in case of failed password, so that failedlogin
event will be dispatched anyway, but depending on your configuration,
it might be required.
Upvotes: 7
Reputation: 310957
This is a connect timeout: there is no Telnet server running at the target IP:port, or that port is blocked in the target firewall.
Upvotes: 0