ShellKit

为 Swift NIO 应用程序提供访问本地 Shell 和通过 SSH 访问远程 Shell 的能力

使用 SPM 安装

.package(url: "https://github.com/Einstore/ShellKit.git", from: "1.0.0")

用法

连接到本地终端

let shell = try Shell(.local, on: eventLoop)
let futureResponse = shell.run(bash: "ls -a").map { output in
   print(output)
   return output
}

连接到远程服务

let shell = try Shell(
    .ssh(
        host: "1292.168.1.2",
        username: "root",
        password: "sup3rS3cr3t"
    ),
    on: eventLoop
)
let futureResponse = shell.run(bash: "ls -a")

还有其他 SSH 身份验证方式可用!

示例

let eventLoop = EmbeddedEventLoop()
let shell = try Shell(.local, on: eventLoop)
let futureResponse = shell.run(bash: "cd /tmp/ ; pwd").map { output in
   print(output)
   return output
}.flatMapError { error in
   print(error)
   return error.localizedDescription
}
let out: String = try futureResponse.wait()
print(out)

使用 CommandKit

通过以下方式访问命令

示例

shell.cmd.pwd().map { currentPath in
   print(currentPath)
}

shell.cmdshell.cmd.install 下发现更多命令

作者

Ondrej Rafaj @rafiki270

许可

MIT;版权所有 2019 - Einstore