swift - 如何使用 swift 4 和 Playgrounds 运行诸如“bash -c ssh ...”之类的程序并与之交互?
问题描述
如何使用 swift 4 和 Playgrounds 在 bash 终端中运行诸如 ssh 之类的程序并与之交互?终端命令可以在 Mac swift Playground 中执行,代码如下:
@discardableResult func shell(_ command: String) -> String {
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}
// Example usage:
let t = shell("ls")
print("\(t)") //prints dir listing
但是,我希望能够与 ssh 之类的程序与 ls 之类的非交互式程序进行交互。例如命令:
let t = shell("ssh xxx.xxx.xxx.xxx -T -l root -p 22")
将启动 ssh 并且 ssh 将通过终端提示输入密码。我希望能够以编程方式提供密码并继续。在我看来,由于 SSH 在终端上下文中运行,劫持终端 stdin 和 stdout 就足够了,但到目前为止我还没有成功。以下是我发现的一些资源,可提供有关劫持的一些信息:
关于是否以及如何实现这一点的任何想法?
解决方案
推荐阅读
- python - 如何将格式 yyyy-mm-ddThh:mm:ss 转换为 unix 时间戳?
- qt - 在没有glOrtho的QOpenGLWidget中放大光标位置
- php - 2个脚本交互获取图片和描述
- typescript - 如何在 TypeScript Playground 中修改 compilerOptions.lib?
- npm - 在哪里获得有关受损 npm 包的警报
- android - 我已经从 WCF 命名空间替换了 tempuri.org,但我可以同时使用吗?
- ios - 在实际 iPhone 上测试时没有触觉反馈
- go - 如何使用golang中的GORM框架将结构数组嵌入另一个结构并将其保存在数据库中
- oracle - 从 Oracle Database 11g 生成表创建脚本
- mysql - 相同的查询和数据结构,但 MySQL 8 与 MySQL 5 返回的结果不同?