An SSH client for Avin, binding libssh2 directly through Avin's C bindings, which check every binding against libssh2's own headers when the program is built.
Every session runs libssh2 non-blocking: a call that would wait for the server parks the calling
thread on the runtime's poller instead, so hundreds of sessions run at once on lightweight threads, one
each, without holding a worker apiece. Every wait is a cancellation point, so a withTimeout or
withDeadline around the work bounds it.
Needs Avin 0.0.32 or later.
The package finds libssh2 with pkg-config (the module libssh2), version 1.11 or later:
- macOS:
brew install libssh2 - Debian and Ubuntu:
apt install libssh2-1-dev
Name it in your package's avin.hocon:
dependencies {
ssh { git = "github.com/avin-lang/ssh", version = "0.0.2" }
}and import from the module dev.avinlang.ssh.
Runs uname -a on a host, logged in with the SSH agent, the host key checked against
~/.ssh/known_hosts:
import avin.time.seconds
import dev.avinlang.ssh.{Options, Session, SshError}
uname(
host: string,
user: string,
port: int = 22,
options: Options = Options(connectTimeout = Some(10.seconds))
) -> Result[string, SshError] =
val session = Session.connect(host, port, options)?
defer _ = session.close()
session.authAgent(user)?
val out = session.run("uname -a")?
Ok(out.stdoutText.trim)
main(args: Vector[string])
uname(args(0), args(1)) match
Ok(text) -> println(text)
Err(e) -> println(s"${args(0)}: ${e.message}")
Output streamed as it arrives, standard output and standard error apart, and the exit status after:
import dev.avinlang.ssh.{Exited, Session, Signalled, SshError, Stderr, Stdout}
follow(session: Session, command: string) -> Result[int, SshError] =
val running = session.exec(command)?
defer _ = running.close()
loop
running.next()? match
Some(Stdout(bytes)) -> print(string.fromUtf8Lossy(bytes))
Some(Stderr(bytes)) -> print(s"! ${string.fromUtf8Lossy(bytes)}")
None -> break
running.wait()? match
Exited(code) -> Ok(code)
Signalled(signal, _) -> Ok(128)
The same command on many hosts at once, each on a lightweight thread of its own, each given ten seconds:
import avin.threads.{scope, withTimeout}
import avin.time.seconds
import dev.avinlang.ssh.{Options, Session, SshError}
onEach(
hosts: Vector[string],
user: string,
command: string,
port: int = 22,
options: Options = Options()
) -> Vector[(string, string)] =
scope(sc ->
val threads = hosts.map(host -> sc.spawn(() ->
val answer = withTimeout(10.seconds, _ -> runOn(host, port, user, command, options)) match
Some(Ok(text)) -> text
Some(Err(e)) -> s"failed: ${e.message}"
None -> "timed out"
(host, answer)))
threads.map(t -> t.join()))
runOn(host: string, port: int, user: string, command: string, options: Options) -> Result[string, SshError] =
val session = Session.connect(host, port, options)?
defer _ = session.close()
session.authAgent(user)?
Ok(session.run(command)?.stdoutText)
A script uploaded over SFTP, then run; a transfer holds one 64 KiB piece at a time, whatever its size:
import dev.avinlang.ssh.{Session, SshError}
deploy(session: Session, script: string, remote: string) -> Result[int, SshError] =
val sftp = session.sftp()?
defer _ = sftp.close()
sftp.putFile(script, remote, 0o755)?
val out = session.run(remote)?
sftp.remove(remote)?
Ok(if out.success then 0 else 1)
A host named in an SSH configuration, SshConfig.load() reading ~/.ssh/config, reached through the
jump host the configuration gives it, and a command run there on a terminal, as ssh -t web sudo …
runs it for hosts whose sudoers ask for one:
import dev.avinlang.ssh.{Pty, SshConfig, SshError, Stdout}
onTerminal(config: SshConfig, alias: string, command: string) -> Result[string, SshError] =
val session = config.connect(alias)?
defer _ = session.close()
val running = session.exec(command, pty = Some(Pty("xterm", 120, 40)))?
defer _ = running.close()
val out = Buffer[byte]()
loop
running.next()? match
Some(Stdout(bytes)) -> out.addAll(bytes)
Some(_) -> ()
None -> break
Ok(string.fromUtf8Lossy(out.toArray()))
A local port forwarded to a database the server can reach, as ssh -L 5433:db.internal:5432 does for
one connection at a time, each relayed both ways until both ends finish:
import avin.net.TcpListener
import dev.avinlang.ssh.{Local, Session, SshError}
forward(session: Session, localPort: int, host: string, port: int) -> Result[unit, SshError] =
val listener = TcpListener.bind("127.0.0.1", localPort).mapErr(e -> Local(e))?
defer _ = listener.close()
loop
val conn = listener.accept().mapErr(e -> Local(e))?
val tunnel = session.tunnel(host, port)?
_ = tunnel.relay(conn)?
_ = tunnel.close()
_ = conn.close()
- Sessions:
Session.connect(host, port, options)makes the TCP connection, shakes hands and checks the host key;Optionsset the connect timeout (connection, handshake and host key together), the timeout of every later wait, the host-key policy, the known-hosts file, keepalives and how longclosewaits. A session is anio.Close:close()closes every command and SFTP subsystem still open, says goodbye and gives the connection back. - Host keys:
HostKeys.Strict(the default) takes only a key the known-hosts file lists for the host and port,AcceptNewadds a host it does not list to the file, andPinned(fingerprint)takes exactly the key with that SHA-256 fingerprint. A key the file does not list isUnknownHostKey, a different oneChangedHostKey, each with the server's fingerprint.hostKeyFingerprint()answers it asssh-keygen -lwrites it. - Logging in:
authAgent(user)(the agentSSH_AUTH_SOCKnames, or one at a path),authKey(user, privateKeyPath, passphrase)andauthPassword(user, password);authMethods(user)answers what the server offers. A refusal isAuthFailed(method, offered); a key file that cannot be read or whose passphrase is wrong isKeyFile. - Commands:
exec(command, stderr)answers a runningExec:next()gives each piece of output as it arrives,StdoutorStderr(or standard error merged or dropped),stdin(bytes)andcloseStdin()feed its input (and it is anio.ByteWriter),wait()answersExited(code)orSignalled(signal, message), andclose()ends it. Several run on one session at once.run(command, limit)runs one to its end and answers anOutputwith at mostlimitbytes of each stream kept. - Terminals:
exec(command, pty = Some(Pty(term, columns, rows, modes)))runs a command on a pseudo-terminal, asssh -tdoes, the terminal modes set first (TerminalMode.Echoand the rest);shell()starts the login shell, interactive on a terminal by default, or reading commands without one;resize(columns, rows)tells a running one its terminal's new size. Under a terminal the command's standard error arrives merged into its standard output, as it does throughssh -t. A terminal the server refuses isChannelRefused. - Tunnels:
tunnel(host, port)asks the server to connect to a host and port as it sees them (direct-tcpip, whatssh -Landssh -Juse) and answers aTunnel, anio.Readerandio.ByteWriterwithreceive,send,closeWrite(a half-close) andclose.relay(conn)joins it to a local connection both ways until both ends finish. A host or port the server cannot reach, or forwarding it forbids, isChannelRefused. Several tunnels and commands run on one session at once. Listening on the server's side (ssh -R) is not bound. ~/.ssh/config:SshConfig.load(),read(path)andparse(text)read a configuration the waysshdoes (Hostpatterns with*,?and!, the first value winning,Includewith globs,Match host,userandall, comments,=and quotes);host(alias)answers itsHostName,User,Port,IdentityFiles,ProxyJump,StrictHostKeyChecking,UserKnownHostsFile,ConnectTimeout,ServerAliveIntervalandIdentitiesOnlyfor a host,~and the%tokens expanded;connect(alias)makes the session, through its jump hosts, and logs in with the agent and then the identity files. A line it cannot use, aMatchcriterion other than those three among them, isConfig(path, line, reason).- SFTP:
sftp()starts the subsystem;upload(reader, remotePath, mode)anddownload(remotePath, writer)stream anyio.Readerup and into anyio.ByteWriter, andputFile/getFiledo the same between local and remote files.stat,mkdir,removeandremoveDirwork on remote paths. The server's refusal isSftpFailed(path, status, reason)(a missing file or directory is status 2); a failing local side isLocal(ioError). - Errors: every failure is an
SshError;safeToRetrysays which came before anything but the connection's own setting up reached the server.
A session and its commands belong to the thread that made them: libssh2 lets one thread at a time
use a session, and using either from another thread traps. A wait that a timeout or a cancellation
cuts off leaves the protocol half way through something, so the session then answers Interrupted
until it is closed.
Many sessions with one server are opened a bounded number at a time. A connection the server has not
accepted yet waits in its listening socket's queue, and the kernel resets one that finds the queue
full: OpenSSH's sshd asks for 128, and macOS caps every queue at kern.ipc.somaxconn, 128 by
default. Two hundred connections at once to an sshd on the same Mac see well over a hundred
resets, as Connect or Handshake errors. Taking a Semaphore permit around Session.connect
alone keeps the sessions themselves all open at once:
val connecting = Semaphore(64)
val s = connecting.withPermit(() -> Session.connect(host))?
The tests run against a private, unprivileged sshd of their own, on 127.0.0.1 port 22220, with its
own host key, authorized keys and SSH agent. Start it from a directory with a short path, /tmp/…:
the agent's socket lives in it, and a socket's path may not be longer than about a hundred bytes.
scripts/test-sshd.zsh start
eval "$(scripts/test-sshd.zsh env)"
avin test
scripts/test-sshd.zsh stopA test that needs the server and finds no SSH_TEST_DIR says it is skipped and passes.