简体   繁体   English

macOS-使用“ Process”将其远程登录到IP地址并传递命令

[英]macOS - Use `Process` to telnet to an IP address and pass commands

I'm developing a macOS app where I need to connect using telnet to a device and send commands to it. 我正在开发一个macOS应用程序,该应用程序需要使用telnet连接到设备并向其发送命令。

(BTW, I'm connecting to a Roku) (顺便说一句,我正在连接Roku)

This is as far as I got 据我所知

func telnet(to host: String, port: String) {
    let process = Process()
    let pipe = Pipe()

    process.executableURL = URL(fileURLWithPath: "/usr/local/bin/telnet")
    process.arguments = [host, port]
    process.standardOutput = pipe

    do {
        try process.run()
    } catch {
        return
    }

    let resultData = pipe.fileHandleForReading.readDataToEndOfFile()
    let result = String (data: resultData, encoding: .utf8) ?? ""
    print(result)
}

telnet(to: "10.0.1.11", port: "8080")

The problem is that the Process is closed immediately. 问题是该流程立即关闭。 I need to leave the Process open and somehow send commands to the device and then close the connection. 我需要将进程保持打开状态,并以某种方式将命令发送到设备,然后关闭连接。

So those are my questions: 这些是我的问题:

  1. How can I leave the Process running? 如何使流程继续运行?
  2. How can I send commands to the device through the telnet connection? 如何通过telnet连接将命令发送到设备?

Your best bet will be to avoid using Process and make a network connection directly from your application. 最好的选择是避免使用Process并直接从您的应用程序建立网络连接。 (This will also free you from your dependency on /usr/local/bin/telnet , which is not present in a clean install of macOS.) (这还将使您摆脱对/usr/local/bin/telnet依赖,该依赖关系在macOS的全新安装中不存在。)

The SwiftSocket pod looks like an excellent way of doing this. SwiftSocket容器看起来是一种很好的方法。 The "Client socket example" in the documentation appears to cover most of your questions. 文档中的“客户端套接字示例”似乎涵盖了您的大多数问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM