Skip to content

Telnet & TCP

Both are byte-stream protocols. D.device.sendTelnetCommand handles Telnet-style line-buffered command/response; D.device.sendTCPCommand is a lower-level send-bytes-and-collect-bytes primitive for devices that speak a custom text or binary protocol.

D.device.sendTelnetCommand({
command: 'show interfaces',
username: 'admin',
password: 'secret',
shell_prompt: '#',
login_prompt: 'login:',
password_prompt: 'Password:',
timeout: 5000
}, function (output, error) {
if (error) return D.failure(D.errorType.RESOURCE_UNAVAILABLE);
console.log(output);
});

Key options:

  • shell_prompt — the runner waits for this string before sending command (and before returning).
  • login_prompt / password_prompt — if these are set, the runner sends username / password when they appear.
  • timeout — ms; on zero output within this window, the runner gives up.

Telnet sends credentials in cleartext. Prefer SSH when the device supports it.

D.device.sendTCPCommand({
command: '?\r\n',
port: 4242,
timeout: 500,
keepAlive: false,
encoding: 'utf8'
}, function (output, error) {
if (error) return D.failure(D.errorType.RESOURCE_UNAVAILABLE);
console.log(output);
});

Key options:

  • command — the exact string to write on the socket. Include terminators (\r\n, \n, NUL) if the protocol needs them.
  • port — defaults to 23 (Telnet); override for custom services.
  • timeout — the runner waits this many ms after the last byte received, then closes and returns.
  • keepAlive — if true the socket stays open until the timeout expires; useful for protocols that send multiple response chunks.
  • encoding — how to decode the returned bytes. Set to 'binary' for non-text protocols and parse the Buffer yourself.

For pure binary exchanges, set encoding: 'binary', send raw bytes in command, and decode the output (which comes back as a binary string in that mode):

D.device.sendTCPCommand({
command: String.fromCharCode(0x01, 0x02, 0x03),
port: 1234,
encoding: 'binary',
timeout: 200
}, function (output, error) {
if (error) return D.failure(D.errorType.RESOURCE_UNAVAILABLE);
var bytes = [];
for (var i = 0; i < output.length; i += 1) bytes.push(output.charCodeAt(i));
// ... inspect bytes
});
D.device.sendTelnetCommand({ ... }, function (output, error) {
if (error) {
if (error.code === 'ECONNREFUSED') return D.failure(D.errorType.RESOURCE_UNAVAILABLE);
if (error.code === 'ETIMEDOUT') return D.failure(D.errorType.TIMEOUT_ERROR);
if (error.code === 'AUTHENTICATION_FAILED') return D.failure(D.errorType.AUTHENTICATION_ERROR);
return D.failure(D.errorType.GENERIC_ERROR);
}
});