Skip to content

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 ssh from "@std/net/ssh"
NameKindDescription
SSHstructSSH connection type.

Creates an SSH connection object.

Arguments:

NameTypeDefaultDescription
hoststringnoneHostname or IP address.
portintnoneSSH port.
userstringnoneSSH username.
shellbooltrueWhether 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)

Connects to the SSH server.

Arguments:

NameTypeDefaultDescription
passwordstring?nilOptional password authentication.
keyFilestring?nilOptional PEM/private key file path.
timeoutMsint5000Connection 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)

Writes text to the SSH session stdin.

Arguments:

NameTypeDescription
datastringData to send.

Returns: void

conn.write("ping\n")

write requires the connection to be open.

Reads one line from the SSH session stdout.

Arguments:

NameTypeDescription
timeoutMsintRead 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.

Runs a command on the SSH session and returns combined output.

Arguments:

NameTypeDescription
cmdstringCommand to run.

Returns: string

const output = conn.run("whoami")
println(output)

run requires the connection to be open.

Returns whether the SSH connection is currently open.

Returns: bool

if conn.state() {
println("connected")
}

Closes the SSH session and client.

Returns: void

conn.close()

Prefer defer after connecting.

conn.connect(password: "password")
defer conn.close()
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)
}
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)

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()
}