Skip to content

k.net.telnetClient ​

Telnet sessions

Execution ContextPrerequisitesTypical Use
KScript API, background jobsReachable host and port speaking plain TCP/TelnetSMTP/text-protocol probing and legacy device command interaction

k.net.telnetClient.connect establishes a Telnet connection and reads and writes terminal data inside the callback via ConnectedClient.

Quick Start ​

js
let reply = ""
k.net.telnetClient.connect("smtp.example.com", 25, (client) => {
  client.terminatedRead("\r\n", 10)
  client.writeLine("NOOP")
  reply = client.read(10)
}, 30)
return { reply }

Quick Reference ​

Method / PropertyDescriptionReturns
connect(host, port, callback[, timeout])Connect to a Telnet host and port and run callbackvoid
client.write(command)Write raw command text without a trailing newlinevoid
client.writeLine(command)Write command text followed by a newlinevoid
client.read([timeout])Read available outputstring
client.terminatedRead(terminator[, timeout])Read output until a matching terminator is reachedstring

connect() ​

Connect to a host and port and run interactive reads/writes via ConnectedClient; the session closes when the callback finishes.

Exceptions are swallowed silently

Unlike sshClient and sftpClient, k.net.telnetClient.connect() catches and silently ignores all connection, timeout, and callback exceptions without throwing or returning an error status. Track completion with a flag set inside callback when failure detection is needed.

ParameterTypeRequiredDescription
hoststringYesTelnet host name or IP
portnumberYesPort
callback(client) => voidYesSession callback after connecting
timeoutnumberNoTimeout in seconds; default is 30

Returns: void.

js
let completed = false
k.net.telnetClient.connect(host, port, (client) => {
  client.writeLine("help")
  const text = client.read(10)
  completed = true
}, 30)

ConnectedClient ​

Terminal I/O object passed to the connect() callback.

MethodParametersReturnsDescription
write(command)command: stringvoidSend a command
writeLine(command)command: stringvoidSend a command line
read(timeout?)timeout?: numberstringRead output
terminatedRead(terminator, timeout?)terminator: string, timeout?: numberstringRead until a terminator is found
js
let hello = ""
let response = ""
k.net.telnetClient.connect("smtp.example.com", 25, (client) => {
  hello = client.terminatedRead("\r\n", 10)
  client.writeLine("HELO kooboo.local")
  response = client.read(10)
})
return { hello, response }

TypeScript Definitions ​

ts
interface TelnetClient {
  connect(host: string, port: number, callback: (p1: ConnectedClient) => void, timeout?: number): void;
}

interface ConnectedClient {
  terminatedRead(terminator: string, timeout?: number): string;
  read(timeout?: number): string;
  write(command: string): void;
  writeLine(command: string): void;
}