Home

Awesome

GitHub license Build Status npm Donate Bitcoin/Altcoins npm version

node-telnet-client

A simple telnet client for Node.js

Installation

Locally in your project or globally:

npm install telnet-client
npm install -g telnet-client

Quick start

Async/Await (Node.js >= 7.6.0)

Note: As of version 2.0.0 of this API, native ES6 promises are returned, not Bluebird promises.

'use strict'

const { Telnet } = require('telnet-client');

(async function () {
  const connection = new Telnet()

  // these parameters are just examples and most probably won't work for your use-case.
  const params = {
    host: '127.0.0.1',
    port: 23,
    shellPrompt: '/ # ', // or negotiationMandatory: false
    timeout: 1500
  }

  try {
    await connection.connect(params)
  } catch (error) {
    // handle the throw (timeout)
  }

  const res = await connection.exec('uptime')
  console.log('async result:', res)
})()

Callback-style

const { Telnet } = require('telnet-client')
const connection = new Telnet()

// these parameters are just examples and most probably won't work for your use-case.
const params = {
  host: '127.0.0.1',
  port: 23,
  shellPrompt: '/ # ', // or negotiationMandatory: false
  timeout: 1500
}

connection.on('ready', prompt => {
  connection.exec(cmd, (err, response) => {
    console.log(response)
  })
})

connection.on('timeout', () => {
  console.log('socket timeout!')
  connection.end()
})

connection.on('close', () => {
  console.log('connection closed')
})

connection.connect(params)

Promises

Note: As of version 2.0.0 of this API, native ES6 promises are returned, not Bluebird promises.

const { Telnet } = require('telnet-client')
const connection = new Telnet()

// these parameters are just examples and most probably won't work for your use-case.
const params = {
  host: '127.0.0.1',
  port: 23,
  shellPrompt: '/ # ', // or negotiationMandatory: false
  timeout: 1500
}

connection.connect(params)
  .then(prompt => {
    connection.exec(cmd)
    .then(res => {
      console.log('promises result:', res)
    })
  }, error => {
    console.log('promises reject:', error)
  })
  .catch(error => {
    // handle the throw (timeout)
  })

Generators

const co = require('co')
const toBluebird = require("to-bluebird")
const { Telnet } = require('telnet-client')
const connection = new Telnet()

// these parameters are just examples and most probably won't work for your use-case.
const params = {
  host: '127.0.0.1',
  port: 23,
  shellPrompt: '/ # ', // or negotiationMandatory: false
  timeout: 1500,
  // removeEcho: 4
}

// using 'co'
co(function*() {
  try {
    yield connection.connect(params)
  } catch (error) {
    // handle the throw (timeout)
  }

  const res = yield connection.exec(cmd)
  console.log('coroutine result:', const)
})

// using Promise
bluebird.coroutine(function*() {
  try {
    yield toBluebird(connection.connect(params))
  } catch (error) {
    // handle the throw (timeout)
  }

  let res = yield toBluebird(connection.exec(cmd))
  console.log('coroutine result:', res)
})()

Async/Await

'use strict'

const { Telnet } = require('telnet-client')

process.on('unhandledRejection', error => {
  throw error
})

async function run() {
  let connection = new Telnet()

  // these parameters are just examples and most probably won't work for your use-case.
  let params = {
    host: '127.0.0.1',
    port: 23,
    shellPrompt: '/ # ', // or negotiationMandatory: false
    timeout: 1500
  }

  try {
    await connection.connect(params)
  } catch (error) {
    // handle the throw (timeout)
  }

  let res = await connection.exec(cmd)
  console.log('async result:', res)
}

run()

Problems?

Please do not directly email any node-telnet-client committers with questions or problems. A community is best served when discussions are held in public.

If you have a problem, please search the issues to see if there's existing reports to the issue you're facing and if there's any known solutions.

I also offer professional (paid) support and services, so make sure to contact me for more info.

API

const { Telnet } = require('telnet-client')
const connection = new Telnet()

connection.connect(options) -> Promise

Creates a new TCP connection to the specified host, where 'options' is an object which can include following properties:

Resolves once the connection is ready (analogous to the ready event). Rejects if the timeout is hit.

connection.exec(data, [options], [callback]) -> Promise

Sends data on the socket (should be a compatible remote host's command if sane information is wanted).

The optional callback parameter will be executed with an error and response when the command is finally written out and the response data has been received.

If there was no error when executing the command, 'error' as the first argument to the callback will be undefined.

Command result will be passed as the second argument to the callback.

*** Important notice/API change from 0.3.0 *** The callback argument is now called with a signature of (error, [response])

Options:

connection.send(data, [options], [callback]) -> Promise

Sends data on the socket without requiring telnet negotiations.

Options:

connection.write(data, [options], [callback]) -> Promise

Same as send(…), but data is sent without appending an output record separator.

connection.nextData() -> Promise

Waits for and returns the next available data from the host, as a string value, either one line at a time, or the last-sent incomplete line. When the telnet session has ended, nextData() always returns a Promise that resolves to null.

connection.shell(callback) -> Promise

Starts an interactive shell session. Returns a duplex stream which can be used to read and write data.

connection.getSocket() -> net.Socket

Returns a duplex stream that can be used for connection hopping. See 'sock' option.

connection.end() -> Promise

Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.

connection.destroy() -> Promise

Ensures that no more I/O activity happens on this socket. Only necessary in case of errors (parse error or so).

Event: 'connect'

Emitted when a socket connection is successfully established.

Event: 'ready'

Emitted when a socket connection is successfully established and the client is successfully connected to the specified remote host. A value of prompt is passed as the first argument to the callback.

Event: 'writedone'

Emitted when a write operation for given data is sent to the socket.

Event: 'data'

This is a forwarded 'data' event from core 'net' library. A <buffer> is received when this event is triggered.

Event: 'timeout'

Emitted if the socket times out from inactivity. This is only to notify that the socket has been idle. The user must manually close the connection.

Event: 'failedlogin'

Emitted when the failedLoginMatch pattern is provided and a match is found from the host. The 'destroy()' method is called directly following this event.

Event: 'error'

Emitted when an error occurs. The 'close' event will be called directly following this event.

Event: 'end'

Emitted when the other end of the socket (remote host) sends a FIN packet.

Event: 'close'

Emitted once the socket is fully closed.

Professional support

I offer professional support for node-telnet-client and beyond. I have many years of expertise on building robust, scalable Node.js applications and can help you overcome issues and challenges preventing you to ship your great products. I also excel in software architecture and implementation, being able to provide you with development, planning, consulting, training and customization services. Feel free to contact me so we can discuss how to help you finish your products!

Sponsors

Become a sponsor and get your logo on project's README on GitHub with a link to your site. Feel free to contact me for the arrangement!

Donate

If you love this project, consider donating!

License

This library is licensed under LGPLv3. Please see LICENSE for licensing details.