使用 Swift 的 SSH 连接
SSH connectivity using Swift
最近,我一直在尝试使用 Swift 制作一个(非常)简单的程序,让您通过 SSH 连接到服务器并执行一些文件。不幸的是,我不知道如何在 Swift 应用程序中完全启动 SSH 会话。这是我写的一些代码:
var sshConnectCommand = ["-c", "spawn ssh "+sshUsername+"@"+sshHost+"; expect assword:; send "+sshPassword+"\r"]
func sshIn() {
//starting ssh session
let sshConnect = NSTask()
sshConnect.arguments = [testCmd]
//rerouting output through a pipe
sshConnect.standardOutput = logAppend
//launch!
sshConnect.launch();
}
如您所见,我已使用 NSTask 尝试使用 运行 'expect' 命令输入密码和所有内容。我想尽量避免使用 SSH-keygen,因为它旨在用于用户无权访问的服务器。
所以,总结一下:
您将如何在不使用 SSH-keygen 的情况下连接到 SSH,同时完全保留在应用程序代码中?
编辑:我还应该补充一点,在尝试编译时,我得到了这个错误:
[Swift._SwiftDeferredNSArray fileSystemRepresentation]: unrecognized selector sent to instance 0x600000032500
。我不确定这是什么意思。
我一直在使用类似的东西通过 ssh 进入我的 Raspberry Pi:
func sshIn() {
let task = CommandLine()
task.launchPath = "/usr/bin/ssh"
task.arguments = ["USERNAME@IPADDRESS", "-t", "sudo systemctl stop mediacenter; /opt/vc/bin/tvservice -o"]
task.launch()
}
-t
命令完成后关闭连接 运行,你可以像这样传递所有命令 command1; command 2
就像我得到的 sudo systemctl stop mediacenter; /opt/vc/bin/tvservice -o
至于注册机的事情,我认为你没有太多选择。不过,您可以考虑将其锁定一点。 Here's 开始寻找的好地方。
最近,我一直在尝试使用 Swift 制作一个(非常)简单的程序,让您通过 SSH 连接到服务器并执行一些文件。不幸的是,我不知道如何在 Swift 应用程序中完全启动 SSH 会话。这是我写的一些代码:
var sshConnectCommand = ["-c", "spawn ssh "+sshUsername+"@"+sshHost+"; expect assword:; send "+sshPassword+"\r"]
func sshIn() {
//starting ssh session
let sshConnect = NSTask()
sshConnect.arguments = [testCmd]
//rerouting output through a pipe
sshConnect.standardOutput = logAppend
//launch!
sshConnect.launch();
}
如您所见,我已使用 NSTask 尝试使用 运行 'expect' 命令输入密码和所有内容。我想尽量避免使用 SSH-keygen,因为它旨在用于用户无权访问的服务器。
所以,总结一下: 您将如何在不使用 SSH-keygen 的情况下连接到 SSH,同时完全保留在应用程序代码中?
编辑:我还应该补充一点,在尝试编译时,我得到了这个错误:
[Swift._SwiftDeferredNSArray fileSystemRepresentation]: unrecognized selector sent to instance 0x600000032500
。我不确定这是什么意思。
我一直在使用类似的东西通过 ssh 进入我的 Raspberry Pi:
func sshIn() {
let task = CommandLine()
task.launchPath = "/usr/bin/ssh"
task.arguments = ["USERNAME@IPADDRESS", "-t", "sudo systemctl stop mediacenter; /opt/vc/bin/tvservice -o"]
task.launch()
}
-t
命令完成后关闭连接 运行,你可以像这样传递所有命令 command1; command 2
就像我得到的 sudo systemctl stop mediacenter; /opt/vc/bin/tvservice -o
至于注册机的事情,我认为你没有太多选择。不过,您可以考虑将其锁定一点。 Here's 开始寻找的好地方。