Open In App

How to connect to Telnet server from Node.js ?

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to connect the Telnet server with the Node.js application.

Telnet: The telnet is an application protocol over the Transmission Control Protocol (TCP) used on the internet or local area network to provide a bidirectional interactive text-oriented communication facility using a virtual terminal connection. Telnet is used to share a terminal with someone far from us. They provide lots of benefits like virtual terminals and real-time execution on command on the shared terminal. if you have more curious about the telnet refer to this article.

NodeJS: Node.js is a runtime environment for javascript, based on the V8 engine that executes javascript on the server side. So with the help of nodejs we also create an event-driven application, if you have more curious about telnet refer to this article.

Program Approach:

  • Firstly declare the same global variable for further use.
  • Import or require two modules for work with a telnet server, first one is the net module the help to manage the network and the second one telnet-stream is 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 Node.js

Step 1: Run the below command for initializing npm.

npm init -y

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

Step 2: install some packages.

npm install telnet-stream

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

Step 3: Create a file with the below code. Here, the filename is index.js

Javascript




// 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.



Similar Reads

How to connect the components using Connect() in React Redux
In React Redux, you use the connect() function to connect your components to the Redux store. This function takes two parameters: mapStateToProps and mapDispatchToProps. mapStateToProps maps the store's state to the component's props, while mapDispatchToProps maps dispatch actions to props. By using connect( ), your components can access the store'
3 min read
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 above and described below: Path/URL: The Path
2 min read
Node.js Connect Mysql with Node app
In this article, we will learn how to connect the Mysql database to the NodeJs application. Before we go deep into the coding part, let’s get a brief introduction about these technologies: NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome's V8 JavaScript engine. It can be dow
3 min read
How to connect SQL Server database from JavaScript in the browser ?
There is no common way to connect to the SQL Server database from a JavaScript client, every browser has its own API and packages to connect to SQL Server. For example, in the Windows operating system, Internet Explorer has a class name called ActiveXObject which is used to create instances of OLE Automation objects, and these objects help us to cr
3 min read
Nodejs - Connect MongoDB with Node app using MongooseJS
Connect the MongoDB database using MongooseJS to our NodeJS application Before we dive into looking how the mongoDB connects with a nodejs app using mongooseJS, lets get a brief intro to these technologies which are ruling the web development domain of today. Node: Node.js (Node) is an open source development platform for executing JavaScript code
3 min read
How to connect Node.js application to MySQL ?
Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In this article, we will discuss how to connect the Node.js application to MySQL. For connecting the node.js with MySQL database
2 min read
How to Display Flash Messages using connect-flash Module in Node.js ?
Connect-flash module for Node.js allows the developers to send a message whenever a user is redirecting to a specified web-page. For example, whenever, a user successfully logged in to his/her account, a message is flashed(displayed) indicating his/her success in the authentication.Prerequisites Before starting with the application, you must have t
3 min read
Node.js socket.connect() Method
The socket.connect() method is an inbuilt application programming interface of class Socket within dgram module which is used to connect the particular server to a particular port and address. Syntax: const socket.connect(port[, address][, callback]) Parameters: This method accepts the following parameters: port: This parameter holds the port numbe
3 min read
Node.js tls.connect() Method
The tls.connect() method is an inbuilt application programming interface of class TLS within tls module which is used to create a tls.TLSSocket object. Syntax: const tls.connect(port[, host][, options][, callback]) Parameters: This method accept four parameters as mentioned above and described below: port: It is the port number.host: It is the name
3 min read
Node.js http2.connect() Method
The http2.connect() is an inbuilt application programming interface of class http2 within the http2 module which is used to return a ClientHttp2Session instance. Syntax: const http2.connect(authority[, options][, listener]) Parameters: This method takes the following argument as a parameter: authority: It is the URL representing a remote HTTP/2 ser
3 min read