Open In App

How to Connect to Telnet Server from Node.js ?

Last Updated : 20 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Connecting to a Telnet server from Node.js involves creating a client that can communicate using the Telnet protocol. Telnet is a protocol used to establish a connection to a remote host to execute commands and transfer data. In Node.js, you can use various packages to facilitate the connection to a Telnet server. One of the most popular packages for this purpose is telnet-client.

Approach

  • Firstly declare the same global variable for further use.
  • Import or require two modules for working with a telnet server. The first one is the net module, which helps manage the network, and the second one is telnet-stream, used for establishing a connection with a telnet server.
  • Initialize the net socket object with the IP and port number of the telnet server.
  • Create a telnet client and connect this above socket.
  • After connection, the socket listing some events is as close for if connections are closed and data for collecting data from the telnet server last but not least do and will event to occur every new step in telnet server create a log.

Connect the Telnet server from NodeJS

Step 1: Run the below command for initializing npm.

npm init -y

Note: -y is used for all settings are default.

Step 2: Install all the required packages.

npm install telnet-stream

Note: telnet-stream is used to connect the telnet to our node application.

The updated dependencies in package.json file will look like:

"dependencies": {
"telnet-stream": "^1.1.0"
}

Example: Implementation to write the code to connect to telnet server from nodeJS.

Node
// index.js 

// Some global variable for further use
const TelnetSocket, net, socket, tSocket;

// Require the net module for work with networking
net = require("net");

// Require and create a TelnetSocket Object
({ TelnetSocket } = require("telnet-stream"));

// Initialize the socket with the our ip and port
socket = net.createConnection(22, "test.rebex.net");

// Connect the socket with telnet
tSocket = new TelnetSocket(socket);

// If the connection are close "close handler"
tSocket.on("close", function () {
    return process.exit();
});

// If the connection are on "on handler"
tSocket.on("data", function (buffer) {
    return process.stdout.write(buffer.toString("utf8"));
});

// If the connection are occurred something "doing handler"
tSocket.on("do", function (option) {
    return tSocket.writeWont(option);
});


tSocket.on("will", function (option) {
    return tSocket.writeDont(option);
});

// If the connection are send the data "data handler"
process.stdin.on("data", function (buffer) {
    return tSocket.write(buffer.toString("utf8"));
});

Step 4: Run the index.js file.

node index.js

Output: Finally, you have connected your node application to the telnet.

Advanced Configuration and Features

Handling Authentication

If your Telnet server requires authentication, ensure that the username and password fields are filled in the params object. You may also need to adjust the shellPrompt and passwordPrompt according to your server’s settings.

Customizing Connection Parameters

You can customize various parameters in the connection object to suit your Telnet server’s configuration, such as:

  • timeout: Defines the timeout period for the connection.
  • initialLFCR: Sends an initial line feed and carriage return, which may be required for some servers.
  • shellPrompt: Specifies the prompt expected from the server after command execution.

Sending Multiple Commands

You can send multiple commands to the Telnet server by chaining await connection.exec('COMMAND') calls. For example:

// Execute multiple commands
const response1 = await connection.exec('command1');
console.log('Response 1:', response1);

const response2 = await connection.exec('command2');
console.log('Response 2:', response2);

Error Handling

It is important to handle errors gracefully. You can wrap your Telnet interactions in a try-catch block to manage connection issues, authentication failures, or unexpected server responses.

Conclusion

Connecting to a Telnet server from Node.js is made simple with the telnet-client package. This guide walks you through the process of setting up a Node.js project, installing the necessary packages, and writing a script to connect to and interact with a Telnet server. By customizing the parameters and handling errors appropriately, you can create robust Telnet client applications for various purposes, such as network management, remote server monitoring, or automated scripting.


Next Article

Similar Reads