gaetanzanella / swift-ssh-client

High-level SSH client interfaces using SwiftNIO SSH

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift SSH Client

This project provides high-level SSH client interfaces using SwiftNIO SSH.

Requirements

Swift SSH Client is compatible with iOS 13.0+ and macOS 10.5+.

Getting started

SSH is a multiplexed protocol: each SSH connection is subdivided into multiple bidirectional communication channels.

Swift SSH Client reflects this pattern. The first step is to set a connection up:

let connection = SSHConnection(
    host: "my_host",
    port: my_port,
    authentication: SSHAuthentication(
        username: "my_username",
        method: .password(.init("my_password")),
        hostKeyValidation: .acceptAll()
    )
)

try await connection.start()

Once connected, you can start executing concrete SSH operations on child communication channels. As SSH Client means to be a high level interface, you do not directly interact with them. Instead you use interfaces dedicated to your use case.

  • SSH commands
let response = try await connection.execute("echo Hello\n")
// Handle response

for try await chunk in connection.stream("echo World\n") {
    // Handle chunk
}
  • SSH shell
let shell = try await connection.requestShell()
for try await chunk in shell.data {
    // Handle chunk
}
  • SFTP client
let sftpClient = try await connection.requestSFTPClient()

// directories
try await sftpClient.createDirectory(at: "./new")
try await sftpClient.removeDirectory(at: "./new")

// files
let file = try await client.openFile(at: "./new/file.txt", flags: .create)
try await file.write("Hello World!".data(using: .utf8)!)
try await file.close()

// and more

You keep track of the connection state, using the dedicated stateUpdateHandler property:

connection.stateUpdateHandle = { state in
    switch state {
    case .idle, .failed:
        // Handle disconnection
    case .ready:
        // Handle connection start
    }
}

As SSHConnection represents the overall SSH connection, if it ends, all the SSH operations or clients linked to it will end accordingly.

Beta version

Consider the 0.1 version as a beta version. From patch to patch, the project API can change a lot.

License

Swift SSH Client is available under the MIT license. See the LICENSE.txt file for more info.

About

High-level SSH client interfaces using SwiftNIO SSH

License:MIT License


Languages

Language:Swift 98.8%Language:Ruby 0.8%Language:Shell 0.4%