Skip to content

SSH

The ssh method gives a subshell pointed at the remote host. The API works as normal when in the context of a remote machine, although the code executes client side.

Path objects keep track of what machine they are for, and can be mixed and matched. This lets you express operations unambiguously:

1
2
3
4
5
6
7
val localFile = dir / "data-file.txt"
ssh("mike@hq.hydraulic.software") {
    // From local to remote.
    cp(localFile, "file-on-remote-host.txt")
    // And back.
    cp("file-on-remote-host.txt", localFile)
}

Executing remote commands

You can execute commands remotely and access their output locally:

1
2
3
ssh("hq.hydraulic.software") {
    check("hostname".get<String>() == "hq.hydraulic.software")
}

Downloading files

The wget method normally downloads files to the machine where the script is run. For a remote shell, it will download directly to the remote machine, without a roundtrip locally. It does this by executing curl remotely. Progress tracking, error propagation, custom headers and so on work as normal, so the API doesn't change.

Explicit scope and lifetime management

1
2
3
4
5
6
7
val remoteMachine = ssh("hq.hydraulic.software")

val remoteFiles = remoteMachine.ls().toSortedSet()
val localFiles = ls().toSortedSet()
val onBoth: Set<Path> = remoteFiles.intersect(localFiles)

remoteMachine.close()

Threading

SSH sessions are not thread safe.