mkozjak / node-telnet-client

A simple telnet client for Node.js
Other
350 stars 97 forks source link

Question: Work with TeamSpeak Telnet Service #132

Closed SvenC56 closed 5 years ago

SvenC56 commented 5 years ago

Hi,

I'm working on a TeamSpeak3 Server Rest API based on Express. For this purpose I want to include this library.

Typically when I connect to TeamSpeak Telnet Service it looks the following:

TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.

Unknown Command error:

test
error id=256 msg=command\snot\sfound

correct command response:

version
version=3.7.1 build=1553759186 platform=Linux
error id=0 msg=ok

Tbh I'm fairly new to telnet. So I made a lot of trial and error. I prepared the telnet service the following way:

'use strict'

require('dotenv').config();
const Telnet = require('telnet-client');

const host = process.env.HOST;
const port = process.env.PORT;

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

  connection.on('connect', () => {
    console.log('connection established');
  });

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

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

  connection.on('error', () => {
    console.log('error');
  });

  connection.on('ready', () => {
    console.log('connection ready!');
  });

  const params = {
    host,
    port,
    shellPrompt: /\bTS3\s+(.*)\s+(.*)$/,
    negotiationMandatory: false,
    debug: true,
    timeout: 2000,
    echoLines: 0
  };

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

  try {
    let data = await connection.send('KEYS *')
    console.log(data);
  } catch(error) {
    console.error(error);
  }

  try {
    let data = await connection.exec('version')
    console.log(data);
  } catch(error) {
    console.error(error);
  }

  // End Connection when everything is done.
  await connection.end();
}

run();

Now I like to execute the "version" command which is public. When I do connection.exec('version'). I do net get any response. Login is done via the login method (see https://sk-gameservers.de/ts3-server-query-befehle-alle#login).

The console output looks the following:

connection established
TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.

Error: response not received
    at Timeout.setTimeout [as _onTimeout] (C:\Users\svenc56\Dev\teamspeak-node-rest-api\node_modules\telnet-client\lib\index.js:155:47)    at ontimeout (timers.js:424:11)
    at tryOnTimeout (timers.js:288:5)
    at listOnTimeout (timers.js:251:5)
    at Timer.processTimers (timers.js:211:10)
connection closed

I hope anyone can help.

Thank you.

SvenC56 commented 5 years ago

I came along this Issue #78 . I adapted this soultion for my question.

'use strict'

require('dotenv').config();
const Telnet = require('telnet-client');

const host = process.env.HOST;
const port = process.env.PORT;

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

  connection.on('connect', () => {
    console.log('connection established');
  });

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

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

  connection.on('ready', (prompt) => {
    console.log('connected');
  });

  const params = {
    host,
    port,
    shellPrompt: 'Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.',
    negotiationMandatory: false,
    debug: true,
    timeout: 2000,
    irs: '\n\r',
    echoLines: -1
  };

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

  try {
    let data = await connection.exec('version', { shellPrompt: 'error id=0 msg=ok'})
    console.log(data.toString());
  } catch(error) {
    console.error(error);
  }

  try {
    let data = await connection.exec('version', { shellPrompt: 'error id=0 msg=ok'})
    console.log(data.toString());
  } catch(error) {
    console.error(error);
  }

  // End Connection when everything is done.
  await connection.end();
}

run();

Console Outputs looks now the following:

connection established
TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.
version=3.7.1 build=1553759186 platform=Linux
error id=0 msg=ok

version=3.7.1 build=1553759186 platform=Linux
error id=0 msg=ok

connection closed

How do I cut off the empty lines and the first line "Welcome to TeamSpeak..."?

Thank you.

mkozjak commented 5 years ago

Hi, @SvenC56! The only thing you can try is setting echoLines to a positive value when running exec(). I see you've set it initially to -1 which is not really a valid line.

mkozjak commented 5 years ago

Please reopen if needed!