{"id":15854,"library":"telnet-client","title":"Node.js Telnet Client","description":"telnet-client is a straightforward Node.js library for establishing and interacting with Telnet servers. As of its current stable version, 2.2.13, it offers modern asynchronous interfaces using native ES6 Promises and async/await syntax, a significant update from version 2.0.0 which transitioned away from Bluebird promises. It also retains support for callback-style interactions. The library focuses on providing core Telnet client functionality, including connection management, command execution, and event handling for 'ready', 'timeout', and 'close' states. Its release cadence appears to be moderate, with updates addressing compatibility and feature improvements. A key differentiator is its simplicity and explicit handling of shell prompts or the option to bypass negotiation, which is crucial for interacting with diverse Telnet server implementations.","status":"active","version":"2.2.13","language":"javascript","source_language":"en","source_url":"https://github.com/mkozjak/node-telnet-client","tags":["javascript","typescript"],"install":[{"cmd":"npm install telnet-client","lang":"bash","label":"npm"},{"cmd":"yarn add telnet-client","lang":"bash","label":"yarn"},{"cmd":"pnpm add telnet-client","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary class for creating a Telnet connection. Uses named export for both CJS and ESM.","wrong":"const Telnet = require('telnet-client');","symbol":"Telnet","correct":"import { Telnet } from 'telnet-client';"},{"note":"The Telnet class must be instantiated using `new` to create a connection object.","wrong":"const connection = Telnet();","symbol":"Telnet client connection","correct":"const connection = new Telnet();"},{"note":"Import types for Telnet connection parameters for enhanced type safety in TypeScript projects.","symbol":"Type import for parameters","correct":"import type { TelnetParams } from 'telnet-client';"}],"quickstart":{"code":"'use strict';\nimport { Telnet } from 'telnet-client';\n\nasync function runTelnetClient() {\n  const connection = new Telnet();\n\n  // These parameters are just examples. Adjust them for your specific Telnet server.\n  const params = {\n    host: '127.0.0.1', // Replace with your Telnet server's host\n    port: 23,          // Replace with your Telnet server's port (default Telnet is 23)\n    shellPrompt: '/ # ', // Important: Set this to your server's prompt, or negotiationMandatory: false\n    timeout: 3000,       // Connection timeout in milliseconds\n    negotiationMandatory: true, // Set to false if you don't need prompt detection and want to send commands directly\n    // You might also need to configure 'irs' (input response string) and 'ors' (output response string)\n    // depending on how your Telnet server responds and expects input.\n  };\n\n  try {\n    console.log(`Attempting to connect to ${params.host}:${params.port}...`);\n    await connection.connect(params);\n    console.log('Connected to Telnet server.');\n\n    // Example: Execute a command like 'uptime'\n    const command = 'uptime';\n    console.log(`Executing command: \"${command}\"`);\n    const response = await connection.exec(command);\n    console.log(`Command response: ${response.trim()}`);\n\n    // Example: Send multiple commands\n    // await connection.exec('ls -l');\n    // console.log('Executed ls -l');\n\n    connection.end();\n    console.log('Connection closed.');\n  } catch (error: any) {\n    console.error('Telnet connection or command execution error:', error.message);\n    // Ensure the connection is closed even on error\n    if (connection.socket && !connection.socket.destroyed) {\n        connection.end();\n    }\n  }\n}\n\nrunTelnetClient();","lang":"typescript","description":"Demonstrates connecting to a Telnet server, executing a command, and handling potential errors using async/await."},"warnings":[{"fix":"Refactor promise chains to use native `Promise.then()`, `Promise.catch()`, `async/await` syntax, or standard `Promise.all()`/`Promise.race()`.","message":"Since version 2.0.0, the `telnet-client` library transitioned from using Bluebird promises to native ES6 Promises. Code relying on Bluebird-specific utilities (e.g., `.tap()`, `.props()`) will break and needs to be updated to use standard Promise methods or async/await.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Accurately determine the target Telnet server's shell prompt and set it in `params.shellPrompt`, or set `params.negotiationMandatory: false` if direct command sending is acceptable.","message":"Successful command execution relies heavily on correctly configuring `shellPrompt` or setting `negotiationMandatory: false`. If `shellPrompt` is incorrect, the library may not correctly identify when the server is ready for the next command, leading to hung operations or incorrect command responses.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always wrap `await connection.connect(params)` and `await connection.exec(cmd)` in `try...catch` blocks, or add `.catch()` to promise chains. Handle the `timeout` event if using callback style.","message":"Connections can unexpectedly time out or fail. It's crucial to implement robust error handling for the `connection.connect()` and `connection.exec()` calls, typically with `try...catch` for async/await or `.catch()` for promises, to prevent unhandled promise rejections.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Verify the `host` and `port` parameters are correct. Ensure the Telnet server is running and accessible from the client's network location. Check firewall rules on both client and server.","cause":"The client failed to establish a TCP connection to the specified host and port. This often indicates the Telnet server is not running, is on a different IP/port, a firewall is blocking the connection, or there's a network issue.","error":"Error: connect ETIMEDOUT"},{"fix":"Implement proper error handling using `try...catch` around `await connection.connect()` and `await connection.exec()`, or add `.catch()` to your promise chains. Consider increasing the `timeout` value if the server is known to be slow.","cause":"The `timeout` duration specified in connection parameters was exceeded before the expected prompt was received or a connection was established, and the resulting promise rejection was not handled.","error":"UnhandledPromiseRejectionWarning: Telnet connection or command execution error: Timeout"},{"fix":"Ensure the `shellPrompt` parameter exactly matches the prompt string displayed by your Telnet server. Alternatively, set `negotiationMandatory: false` in the connection parameters if you want to bypass prompt detection and send commands immediately.","cause":"The `shellPrompt` parameter is incorrectly configured, leading the client to wait for a prompt that never arrives, or the server's negotiation process is not handled correctly.","error":"Commands don't execute or hang indefinitely after connecting."}],"ecosystem":"npm"}