SSH
The @std/net/ssh package provides an SSH client.
It supports password authentication and private key authentication.
import ssh from "@std/net/ssh"
const conn = ssh.SSH("127.0.0.1", 2222, "testuser", shell: false)
conn.connect(password: "password")defer conn.close()Import
Section titled “Import”import ssh from "@std/net/ssh"Exports
Section titled “Exports”| Name | Kind | Description |
|---|---|---|
SSH | struct | SSH connection type. |
ssh.SSH
Section titled “ssh.SSH”Creates an SSH connection object.
Arguments:
| Name | Type | Default | Description |
|---|---|---|---|
host | string | none | Hostname or IP address. |
port | int | none | SSH port. |
user | string | none | SSH username. |
shell | bool | true | Whether to start an interactive shell after connecting. |
Returns: SSH
import ssh from "@std/net/ssh"
const conn = ssh.SSH("127.0.0.1", 2222, "testuser")Disable shell mode when you only want to run commands or use the connection in a controlled way.
const conn = ssh.SSH("127.0.0.1", 2222, "testuser", shell: false)conn.connect
Section titled “conn.connect”Connects to the SSH server.
Arguments:
| Name | Type | Default | Description |
|---|---|---|---|
password | string? | nil | Optional password authentication. |
keyFile | string? | nil | Optional PEM/private key file path. |
timeoutMs | int | 5000 | Connection timeout in milliseconds. |
Returns: void
Password authentication:
conn.connect(password: "password")Private key authentication:
conn.connect(keyFile: "/home/user/.ssh/id_rsa")With a custom timeout:
conn.connect(password: "password", timeoutMs: 10000)conn.write
Section titled “conn.write”Writes text to the SSH session stdin.
Arguments:
| Name | Type | Description |
|---|---|---|
data | string | Data to send. |
Returns: void
conn.write("ping\n")write requires the connection to be open.
conn.read
Section titled “conn.read”Reads one line from the SSH session stdout.
Arguments:
| Name | Type | Description |
|---|---|---|
timeoutMs | int | Read timeout in milliseconds. |
Returns: string
const out = conn.read(2000)
if out != "" { println("SSH got:", out)}If the timeout is reached before a line is available, read returns an empty string.
conn.run
Section titled “conn.run”Runs a command on the SSH session and returns combined output.
Arguments:
| Name | Type | Description |
|---|---|---|
cmd | string | Command to run. |
Returns: string
const output = conn.run("whoami")
println(output)run requires the connection to be open.
conn.state
Section titled “conn.state”Returns whether the SSH connection is currently open.
Returns: bool
if conn.state() { println("connected")}conn.close
Section titled “conn.close”Closes the SSH session and client.
Returns: void
conn.close()Prefer defer after connecting.
conn.connect(password: "password")defer conn.close()Complete Interactive Example
Section titled “Complete Interactive Example”import ssh from "@std/net/ssh"
const conn = ssh.SSH("127.0.0.1", 2222, "testuser", shell: false)
conn.connect(password: "password")defer conn.close()
while true { const out = conn.read(2000)
if out != "" { println("SSH got:", out) }
conn.write("ping\n") sleep(1000)}Complete Command Example
Section titled “Complete Command Example”import ssh from "@std/net/ssh"
const conn = ssh.SSH("example.com", 22, "deploy", shell: false)
conn.connect(keyFile: "/home/deploy/.ssh/id_rsa")defer conn.close()
const output = conn.run("uptime")
println(output)Function Syntax Note
Section titled “Function Syntax Note”Use -> only when describing a function value type.
let reader: fn(int) -> stringWhen 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()}