ssh
SSH connections, running a command on a server, and working with files over SFTP.
import ssh
This package is an SSH client written in Salam and needs neither OpenSSH nor any other library. Make a Config with the host and the user, pick the login method, and connect with Connect; then send a command with Exec, or move files with SftpOpen. It suits automating deployments and backups.
The host key is checked against the known_hosts file, the way ssh itself does. The buffer and reader functions build and read the data format of the SSH protocol; an ordinary program does not need them.
Types
Config
struct Config| Name | Type | Description |
|---|---|---|
host | str = "" | The host. |
port | int = 22 | The port; 22 by default. |
user | str = "" | The user name. |
password | str = "" | The password. |
use_password | bool = false | Log in with a password. |
key_pem | str = "" | The private key as text. |
use_key | bool = false | Log in with a key. |
hostkey_mode | int = 1 | The host key check mode. |
known_hosts_path | str = "" | The path of the known_hosts file. |
import ssh
func main:
mut cfg := ssh.NewConfig("example.com", ssh.DefaultPort, "deploy")
println cfg.host, cfg.port, cfg.user, cfg.hostkey_mode == ssh.HOSTKEY_MODE_ACCEPT_NEW
ssh.WithPassword(cfg, "s3cret")
println cfg.use_password, cfg.use_key
ssh.WithKeyPEM(cfg, "-----BEGIN OPENSSH KEY-----")
println cfg.use_key, cfg.key_pem.len() > 0
println ssh.WithKeyFile(cfg, "/nonexistent/id_rsa")
ssh.WithHostKeyMode(cfg, ssh.HOSTKEY_MODE_STRICT)
ssh.WithKnownHostsPath(cfg, "/home/deploy/.ssh/known_hosts")
println cfg.hostkey_mode, cfg.known_hosts_path
println ssh.HOSTKEY_MODE_INSECURE, ssh.ClientVersion
endSession
struct Session| Name | Type | Description |
|---|---|---|
ch | Channel | The SSH channel. |
ok | bool = false | The session opened. |
import ssh
func main:
mut cfg := ssh.NewConfig("server.example.com", 22, "deploy")
_ := ssh.WithKeyFile(cfg, "/home/deploy/.ssh/id_rsa")
mut client := ssh.Connect(cfg)
if not ssh.Ok(client):
println "error:", ssh.Error(client)
ret
end
println "host key:", ssh.HostKeyFingerprint(client)
r := ssh.Exec(client, "uname -a")
println r.ok, r.exit_code, r.stdout, r.stderr
r2 := ssh.RunCommand(client, "df -h /")
println r2.stdout
mut tail := ssh.NewSession(client)
_ := ssh.RequestPty(tail, client, "xterm", 120, 40)
if ssh.Start(tail, client, "tail -f /var/log/app.log"):
repeat 10:
_ := ssh.ReadStep(tail, client)
print ssh.DrainOutput(tail)
print ssh.DrainStderrOutput(tail)
end
end
ssh.CloseSession(tail, client)
mut shell := ssh.NewSession(client)
if ssh.Shell(shell, client):
_ := ssh.WriteInput(shell, client, "echo hello\nexit\n")
ssh.CloseInput(shell, client)
code := ssh.Wait(shell, client)
println code, ssh.ExitCode(shell), ssh.SessionClosed(shell)
println ssh.Output(shell), ssh.StderrOutput(shell)
end
ssh.CloseSession(shell, client)
probe := ssh.Dial(cfg)
println ssh.Ok(probe)
println ssh.Reconnect(client)
println ssh.SelfTest()
ssh.Close(client)
endExecResult
struct ExecResult| Name | Type | Description |
|---|---|---|
stdout | str = "" | The output. |
stderr | str = "" | The error output. |
exit_code | int = -1 | The exit code. |
ok | bool = false | The command ran. |
The example for this item is shown with the ones above: ssh-Exec.salam
Sftp
struct Sftp| Name | Type | Description |
|---|---|---|
ch | Channel | The channel. |
ok | bool = false | It opened. |
next_id | u32 = 1 | The number of the next request. |
import ssh
func main:
mut cfg := ssh.NewConfig("files.example.com", 22, "ali")
ssh.WithPassword(cfg, "s3cret")
mut client := ssh.Connect(cfg)
mut sftp := ssh.SftpOpen(client)
if not sftp.ok:
println ssh.Error(client)
ret
end
println ssh.MkdirAll(client, sftp, "/backup/2026/09")
println ssh.Upload(client, sftp, "report.pdf", "/backup/2026/09/report.pdf")
println ssh.UploadDir(client, sftp, "site", "/var/www/site")
if ssh.Exists(client, sftp, "/backup/2026/09/report.pdf"):
info := ssh.Stat(client, sftp, "/backup/2026/09/report.pdf")
println info.name, info.size, info.is_dir, info.permissions
end
each info in ssh.ListDir(client, sftp, "/backup"):
println info.name, info.is_dir
end
println ssh.IsLink(client, sftp, "/current"), ssh.Lstat(client, sftp, "/current").name
_ := ssh.Download(client, sftp, "/etc/hostname", "hostname.txt")
_ := ssh.DownloadDir(client, sftp, "/var/log/app", "logs")
_ := ssh.Rename(client, sftp, "/tmp/a", "/tmp/b")
_ := ssh.Remove(client, sftp, "/tmp/b")
_ := ssh.Mkdir(client, sftp, "/tmp/x")
_ := ssh.Rmdir(client, sftp, "/tmp/x")
_ := ssh.RemoveAll(client, sftp, "/tmp/old")
ssh.SftpClose(client, sftp)
ssh.Close(client)
endSftpFileInfo
struct SftpFileInfo| Name | Type | Description |
|---|---|---|
name | str = "" | The name. |
size | i64 = 0 | The size in bytes. |
is_dir | bool = false | It is a directory. |
permissions | u32 = 0 | The permissions as a Unix number. |
The example for this item is shown with the ones above: ssh-Sftp.salam
Client
struct Client| Name | Type | Description |
|---|---|---|
transport | Transport | The encrypted layer. |
cfg | Config | The configuration. |
connected | bool = false | It is connected. |
errmsg | str = "" | The error message. |
The example for this item is shown with the ones above: ssh-Exec.salam
Constants and variables
DefaultPort
const DefaultPort: i32 = 22The example for this item is shown with the ones above: ssh-Config.salam
ClientVersion
const ClientVersion: str = "SSH-2.0-Salam_1.0"The example for this item is shown with the ones above: ssh-Config.salam
HOSTKEY_MODE_INSECURE
const HOSTKEY_MODE_INSECURE: i32 = 0The example for this item is shown with the ones above: ssh-Config.salam
HOSTKEY_MODE_ACCEPT_NEW
const HOSTKEY_MODE_ACCEPT_NEW: i32 = 1The example for this item is shown with the ones above: ssh-Config.salam
HOSTKEY_MODE_STRICT
const HOSTKEY_MODE_STRICT: i32 = 2The example for this item is shown with the ones above: ssh-Config.salam
Functions
NewConfig
func NewConfig(host: str, port: int, user: str): ConfigThe example for this item is shown with the ones above: ssh-Config.salam
WithPassword
func WithPassword(cfg &: Config, password: str)The example for this item is shown with the ones above: ssh-Config.salam
WithKeyFile
func WithKeyFile(cfg &: Config, path: str): boolThe example for this item is shown with the ones above: ssh-Config.salam
WithKeyPEM
func WithKeyPEM(cfg &: Config, pem: str)The example for this item is shown with the ones above: ssh-Config.salam
WithHostKeyMode
func WithHostKeyMode(cfg &: Config, mode: int)The example for this item is shown with the ones above: ssh-Config.salam
WithKnownHostsPath
func WithKnownHostsPath(cfg &: Config, path: str)The example for this item is shown with the ones above: ssh-Config.salam
SelfTest
func SelfTest(): intThe example for this item is shown with the ones above: ssh-Exec.salam
NewSession
func NewSession(client &: Client): SessionThe example for this item is shown with the ones above: ssh-Exec.salam
RequestPty
func RequestPty(session &: Session, client &: Client, term: str, cols: int, rows: int): boolThe example for this item is shown with the ones above: ssh-Exec.salam
Start
func Start(session &: Session, client &: Client, cmd: str): boolThe example for this item is shown with the ones above: ssh-Exec.salam
Shell
func Shell(session &: Session, client &: Client): boolThe example for this item is shown with the ones above: ssh-Exec.salam
Wait
func Wait(session &: Session, client &: Client): intThe example for this item is shown with the ones above: ssh-Exec.salam
ReadStep
func ReadStep(session &: Session, client &: Client): boolThe example for this item is shown with the ones above: ssh-Exec.salam
Output
func Output(session: Session): strThe example for this item is shown with the ones above: ssh-Exec.salam
StderrOutput
func StderrOutput(session: Session): strThe example for this item is shown with the ones above: ssh-Exec.salam
DrainOutput
func DrainOutput(session &: Session): strThe example for this item is shown with the ones above: ssh-Exec.salam
DrainStderrOutput
func DrainStderrOutput(session &: Session): strThe example for this item is shown with the ones above: ssh-Exec.salam
ExitCode
func ExitCode(session: Session): intThe example for this item is shown with the ones above: ssh-Exec.salam
SessionClosed
func SessionClosed(session: Session): boolThe example for this item is shown with the ones above: ssh-Exec.salam
WriteInput
func WriteInput(session &: Session, client &: Client, data: str): boolThe example for this item is shown with the ones above: ssh-Exec.salam
CloseInput
func CloseInput(session &: Session, client &: Client)The example for this item is shown with the ones above: ssh-Exec.salam
CloseSession
func CloseSession(session &: Session, client &: Client)The example for this item is shown with the ones above: ssh-Exec.salam
Exec
func Exec(client &: Client, cmd: str): ExecResultThe example for this item is shown with the ones above: ssh-Exec.salam
SftpOpen
func SftpOpen(client &: Client): SftpThe example for this item is shown with the ones above: ssh-Sftp.salam
SftpClose
func SftpClose(client &: Client, sftp &: Sftp)The example for this item is shown with the ones above: ssh-Sftp.salam
Upload
func Upload(client &: Client, sftp &: Sftp, local_path: str, remote_path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
Download
func Download(client &: Client, sftp &: Sftp, remote_path: str, local_path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
Mkdir
func Mkdir(client &: Client, sftp &: Sftp, path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
Remove
func Remove(client &: Client, sftp &: Sftp, path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
Rmdir
func Rmdir(client &: Client, sftp &: Sftp, path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
Rename
func Rename(client &: Client, sftp &: Sftp, old_path: str, new_path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
Stat
func Stat(client &: Client, sftp &: Sftp, path: str): SftpFileInfoThe example for this item is shown with the ones above: ssh-Sftp.salam
Exists
func Exists(client &: Client, sftp &: Sftp, path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
ListDir
func ListDir(client &: Client, sftp &: Sftp, path: str): Vector<SftpFileInfo>. and ...The example for this item is shown with the ones above: ssh-Sftp.salam
MkdirAll
func MkdirAll(client &: Client, sftp &: Sftp, path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
RemoveAll
func RemoveAll(client &: Client, sftp &: Sftp, path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
DownloadDir
func DownloadDir(client &: Client, sftp &: Sftp, remote_path: str, local_path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
UploadDir
func UploadDir(client &: Client, sftp &: Sftp, local_path: str, remote_path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
Lstat
func Lstat(client &: Client, sftp &: Sftp, path: str): SftpFileInfoThe example for this item is shown with the ones above: ssh-Sftp.salam
IsLink
func IsLink(client &: Client, sftp &: Sftp, path: str): boolThe example for this item is shown with the ones above: ssh-Sftp.salam
NewBuf
func NewBuf(cap0: int): Bufimport ssh
func main:
mut b := ssh.NewBuf(64)
ssh.BufPutByte(b, 20)
ssh.BufPutUint32(b, 7 as u32)
ssh.BufPutUint64(b, 9 as u64)
ssh.BufPutBool(b, true)
ssh.BufPutStringStr(b, "ssh-ed25519,rsa-sha2-256")
ssh.BufPutString(b, "abc" as void*, 3)
ssh.BufPutBytes(b, "xy" as void*, 2)
ssh.BufPutZeros(b, 2)
ssh.BufPutMpintBytes(b, "\x80" as void*, 1)
println b.len
mut r := ssh.ReaderFromBuf(b)
println ssh.RGetByte(r), ssh.RGetUint32(r), ssh.RGetUint64(r), ssh.RGetBool(r)
names := ssh.RGetNameList(r)
println names, ssh.NameListContains(names, "rsa-sha2-256")
println ssh.RGetStringAsStr(r)
ssh.RSkip(r, 4)
println ssh.RRemaining(r)
n := ssh.RGetMpintView(r)
println n.len, ssh.RRemaining(r)
println ssh.NegotiateFirstMatch("aes256-ctr,aes128-ctr", "aes128-ctr,aes256-ctr")
println ssh.ConstantTimeEqual("abcd" as void*, "abcd" as void*, 4), ssh.ConstantTimeEqual("abcd" as void*, "abce" as void*, 4)
mut b2 := ssh.NewBuf(16)
ssh.BufPutStringBuf(b2, b)
ssh.BufPutMpintBuf(b2, b)
ssh.BufPutBuf(b2, b)
mut r2 := ssh.NewReader(b2.p, b2.len)
view := ssh.RGetStringView(r2)
println view.len == b.len
mut copied := ssh.RGetStringCopy(ssh.ReaderFromBuf(b2))
mut number := ssh.RGetMpintCopy(r2)
println copied.len, number.len, ssh.RRemaining(r2)
ssh.WipeBuf(b)
ssh.FreeBuf(b)
ssh.FreeBuf(b2)
ssh.FreeBuf(copied)
ssh.FreeBuf(number)
endFreeBuf
func FreeBuf(b &: Buf)The example for this item is shown with the ones above: ssh-Wire.salam
WipeBuf
func WipeBuf(b &: Buf)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutByte
func BufPutByte(b &: Buf, v: int)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutBytes
func BufPutBytes(b &: Buf, p: void*, n: int)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutBuf
func BufPutBuf(b &: Buf, src: Buf)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutZeros
func BufPutZeros(b &: Buf, n: int)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutUint32
func BufPutUint32(b &: Buf, v: u32)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutUint64
func BufPutUint64(b &: Buf, v: u64)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutBool
func BufPutBool(b &: Buf, v: bool)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutString
func BufPutString(b &: Buf, p: void*, n: int)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutStringBuf
func BufPutStringBuf(b &: Buf, src: Buf)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutStringStr
func BufPutStringStr(b &: Buf, s: str)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutMpintBytes
func BufPutMpintBytes(b &: Buf, p: void*, n: int)The example for this item is shown with the ones above: ssh-Wire.salam
BufPutMpintBuf
func BufPutMpintBuf(b &: Buf, src: Buf)The example for this item is shown with the ones above: ssh-Wire.salam
NewReader
func NewReader(p: void*, n: int): ReaderThe example for this item is shown with the ones above: ssh-Wire.salam
ReaderFromBuf
func ReaderFromBuf(b: Buf): ReaderThe example for this item is shown with the ones above: ssh-Wire.salam
RRemaining
func RRemaining(r: Reader): intThe example for this item is shown with the ones above: ssh-Wire.salam
RGetByte
func RGetByte(r &: Reader): intThe example for this item is shown with the ones above: ssh-Wire.salam
RGetBool
func RGetBool(r &: Reader): boolThe example for this item is shown with the ones above: ssh-Wire.salam
RSkip
func RSkip(r &: Reader, n: int)The example for this item is shown with the ones above: ssh-Wire.salam
RGetUint32
func RGetUint32(r &: Reader): u32The example for this item is shown with the ones above: ssh-Wire.salam
RGetUint64
func RGetUint64(r &: Reader): u64The example for this item is shown with the ones above: ssh-Wire.salam
RGetStringView
func RGetStringView(r &: Reader): ReaderThe example for this item is shown with the ones above: ssh-Wire.salam
RGetStringCopy
func RGetStringCopy(r &: Reader): BufThe example for this item is shown with the ones above: ssh-Wire.salam
RGetStringAsStr
func RGetStringAsStr(r &: Reader): strThe example for this item is shown with the ones above: ssh-Wire.salam
RGetMpintView
func RGetMpintView(r &: Reader): ReaderThe example for this item is shown with the ones above: ssh-Wire.salam
RGetMpintCopy
func RGetMpintCopy(r &: Reader): BufThe example for this item is shown with the ones above: ssh-Wire.salam
RGetNameList
func RGetNameList(r &: Reader): strThe example for this item is shown with the ones above: ssh-Wire.salam
NameListContains
func NameListContains(list: str, name: str): boolThe example for this item is shown with the ones above: ssh-Wire.salam
NegotiateFirstMatch
func NegotiateFirstMatch(mine: str, theirs: str): strThe example for this item is shown with the ones above: ssh-Wire.salam
ConstantTimeEqual
func ConstantTimeEqual(a: void*, b: void*, n: int): boolThe example for this item is shown with the ones above: ssh-Wire.salam
Dial
func Dial(cfg: Config): ClientThe example for this item is shown with the ones above: ssh-Exec.salam
Connect
func Connect(cfg: Config): ClientThe example for this item is shown with the ones above: ssh-Exec.salam
Ok
func Ok(client: Client): boolThe example for this item is shown with the ones above: ssh-Exec.salam
Error
func Error(client: Client): strThe example for this item is shown with the ones above: ssh-Exec.salam
HostKeyFingerprint
func HostKeyFingerprint(client: Client): strssh shows.The example for this item is shown with the ones above: ssh-Exec.salam
Close
func Close(client &: Client)The example for this item is shown with the ones above: ssh-Exec.salam
Reconnect
func Reconnect(client &: Client): boolThe example for this item is shown with the ones above: ssh-Exec.salam
RunCommand
func RunCommand(client &: Client, cmd: str): ExecResultExec.The example for this item is shown with the ones above: ssh-Exec.salam