Skip to content

Telnet

The @std/net/telnet package provides a simple Telnet client.

import telnet from "@std/net/telnet"
const conn = telnet.Telnet("127.0.0.1", 2323)
defer conn.close()
conn.write("ping\n")
const out = conn.read(2000)
println(out)
import telnet from "@std/net/telnet"
NameKindDescription
TelnetstructTelnet connection type.

Creates and connects a Telnet client.

Arguments:

NameTypeDescription
hoststringHostname or IP address.
portintTCP port.

Returns: Telnet

import telnet from "@std/net/telnet"
const conn = telnet.Telnet("127.0.0.1", 2323)
defer conn.close()

Writes text data to the Telnet connection.

Arguments:

NameTypeDescription
datastringData to send.

Returns: void

conn.write("ping\n")

Reads one line from the Telnet connection.

Arguments:

NameTypeDescription
timeoutMsintRead timeout in milliseconds.

Returns: string

const out = conn.read(2000)
if out != "" {
println("Telnet got:", out)
}

read waits until a newline is received or until the timeout/deadline is reached.

Closes the Telnet connection.

Returns: void

conn.close()

Prefer defer so the connection is closed automatically when the current scope exits.

const conn = telnet.Telnet("127.0.0.1", 2323)
defer conn.close()
import telnet from "@std/net/telnet"
const conn = telnet.Telnet("127.0.0.1", 2323)
defer conn.close()
while true {
const out = conn.read(2000)
if out != "" {
println("Telnet got:", out)
}
conn.write("ping\n")
sleep(1000)
}

Use -> only when describing a function value type.

let reader: fn(int) -> string

When writing a function implementation, put the return type after the parameter list.

fn readOnce(timeoutMs: int) string {
return conn.read(timeoutMs)
}

For a function that returns nothing, use void.

fn closeConn() void {
conn.close()
}