Skip to content

execute output #44

@blackndoor

Description

@blackndoor

How can we get the output result of a command ?

I created a swift file with the following content:

import Foundation
import SwiftSH

class sshShell {
    func executeCMD(cmd:String) -> String {
        let command = Command(host: "192.168.1.50", port: 22)
        var output:String = ""
    
        command!.connect()
               .authenticate(.byPassword(username: "user1", password: "password1"))
               .execute(cmd) { (cmd, result: String?, error) in
                   if let result = result {
                       print("here")
                       print(result)
                       output = result
                   } else {
                       print("there")
                       output =  "ERROR"
                   }
               }
        return output
    }
}

And in the ContentView, I have:

        var output: String = ""
        Button(action: {
            let atest = sshShell()
            output = atest.executeCMD(cmd: "/bin/ls -la")
        }, label: {
            Text("Click me")
        })

        Text(output)
            .padding()

Here is the debugger output:

DEBUG: Timeout set to 10.0 seconds
INFO: Libssh2 v1.9.0
DEBUG: 192.168.1.50 resolved. 1 addresses
INFO: Connection to 192.168.1.50 on port 22 successful
DEBUG: Remote banner is SSH-2.0-OpenSSH_8.6
DEBUG: Fingerprint is BF:...
DEBUG: Supported authentication methods: [Public Key, Password, Keyboard Interactive]
DEBUG: Authenticating by Password
DEBUG: Opening the channel...
DEBUG: Environment: []
INFO: Bye bye
DEBUG: Disconnected

The connection seems OK but no result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions