Skip to content

and-cesbo/swift-ssh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d1932ce ยท Sep 27, 2024

History

54 Commits
Sep 27, 2024
Sep 27, 2024
Sep 17, 2024
Sep 27, 2024
Sep 25, 2024
Sep 27, 2024

Repository files navigation

swift-ssh

Swift wrapper for libssh2 with static build

Dependencies

brew install openssl zlib libssh2

Usage

Connect to a server

let ssh = try await SSH2.connect(
    "example.com",
    port: 22,
    banner: "SSH-2.0-libssh2_Demo"
)

Authentication

Password authentication:

try await ssh.auth(
    "root",
    SSH2AuthMethod.password("password")
)

Private key authentication:

let key = try String(
    contentsOfFile: "/Users/example/.ssh/id_ed25519",
    encoding: .utf8
)

try await ssh.auth(
    "root",
    SSH2AuthMethod.privateKey(key, "passphrase")
)

Ask for passphrase if needed:

var auth = SSH2AuthMethod.privateKey("...")

while true {
    do {
        try await ssh.auth(username, auth)
        break
    } catch {
        switch error {
        case SSH2Error.authFailed(-16, _):
            let passphrase = requestPassphrase()
            auth = .privateKey(key, passphrase)
        default:
            throw error
        }
    }
}

Execute a command

Basic command execution:

let channel = try await ssh.exec("ls -la")
let (stdout, stderr) = try await channel.readAll()

Writing data to the command stdin:

let script = "date"
let data = script.data(using: .utf8)!
let channel = try await ssh.exec("/bin/sh -s")
try await channel.writeAll(data)

Writing from file to the command stdin:

let stdin = Pipe()
let channel = try await ssh.exec("/bin/sh -s")
try await channel.writeAll(stdin.fileHandleForReading)

Reading data from command with handlers:

try await channel.readAll(
    stdoutHandler: {
        if let text = String(data: $0, encoding: .utf8) {
            print(text, terminator: "")
        }
    },
    stderrHandler: {
        if let text = String(data: $0, encoding: .utf8) {
            print(text, terminator: "")
        }
    }
)

Reading data from command to string:

let (stdout, stderr) = try await channel.readAll()

Cancel task

You may cancel task any time:

let task = Task {
    let (stdout, stderr) = try await channel.readAll()
}

task.cancel()

Executed process will keep running on the server side.

About

Swift wrapper for libssh2

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published