How to connect to Telnet server from Node.js ? - GeeksforGeeks

15 Dec, 2021
In this article, we will see how to connect the Telnet server with 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 the 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 the telnet refer to this article.
Program Approach:
Firstly declare the same global variable for further use.
Import or require two modules for work with 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 net socket object with 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 a 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 usevar TelnetSocket, net, socket, tSocket; // Require the net module for work with networkingnet = require("net"); // Require and create a TelnetSocket Object({ TelnetSocket } = require("telnet-stream")); // Initialize the socket with the our ip and portsocket = net.createConnection(22, "test.rebex.net"); // Connect the socket with telnettSocket = 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 somethin "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 teh data "data handler"process.stdin.on("data", function (buffer) {    return tSocket.write(buffer.toString("utf8"));});
Step 4: Run index.js file.
node index.js
Output: Finally, you have connected your node application to telnet.
adnanirshad158
rajeev0719singh
gabaa406
Node.js-process-module
NodeJS-Questions
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to build a basic CRUD app with Node.js and ReactJS ?
How to connect Node.js with React.js ?
Mongoose Populate() Method
Express.js req.params Property
Mongoose find() Function
Top 10 Front End Developer Skills That You Need in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript