How to Connect to Telnet Server from Node.js ?
Last Updated :
20 Jun, 2024
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.
Similar Reads
How to connect mongodb Server with Node.js ?
mongodb.connect() method is the method of the MongoDB module of the Node.js which is used to connect the database with our Node.js Application. This is an asynchronous method of the MongoDB module.Syntax:mongodb.connect(path,callbackfunction)Parameters: This method accept two parameters as mentioned
1 min read
How to Set Online SQL Server for Node.js ?
An Online SQL server helps to connect your project with an online server instead of localhost database setup. To achieve this, the remotemysql site is popular for providing an online database to store our data in a secure way. Installation of sql module: You can visit the link Install sql module. Y
2 min read
How to Create HTTPS Server with Node.js ?
Creating an HTTPS server in Node.js ensures secure communication between your server and clients. HTTPS encrypts data sent over the network, providing a layer of security essential for handling sensitive information. This guide will walk you through the process of setting up an HTTPS server in Node.
4 min read
Node.js http.ServerResponse.connection Method
The httpServerResponse.connection is an inbuilt application programming interface of class Server Response within http module which is used to get the response socket of this HTTP connection. Syntax: response.connection Parameters: This method does not accept any argument as a parameter. Return Valu
2 min read
How to Build a Simple Web Server with Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make
3 min read
How to Create a Simple Server in Node.js that Display Hello World ?
We will create a simple server in Node.js that returns Hello World using an express server. Node.js is a powerful JavaScript runtime built on Chrome's V8 engine, commonly used to build scalable network applications. One of the fundamental tasks when learning Node.js is creating a simple server that
2 min read
How to Connect Node.js to Woocommerce API ?
WooCommerce is one of the most popular e-commerce platforms available, powering over 30% of all online stores. It is built on top of WordPress and provides a powerful API for developers to interact with their store data programmatically. If you are building a Node.js application that interacts with
4 min read
How To Solve Telnet Connection Refused By Remote Host In Linux
When trying to access a remote server over Telnet, encountering a âconnection refusedâ error is common. This issue can disrupt administrative tasks and block access to vital servers, networking equipment, and other devices. Telnet, though considered less secure, is still widely used for managing old
5 min read
How To Create a Simple HTTP Server in Node?
NodeJS is a powerful runtime environment that allows developers to build scalable and high-performance applications, especially for I/O-bound operations. One of the most common uses of NodeJS is to create HTTP servers. What is HTTP?HTTP (Hypertext Transfer Protocol) is a protocol used for transferri
3 min read
How to Open Node.js Command Prompt ?
Node.js enables the execution of JavaScript code outside a web browser. It is not a framework or a programming language, but rather a backend JavaScript runtime environment that allows scripts to be executed outside the browser. You can download Node.js from the web by visiting the link "Download No
2 min read