Frugghi / SwiftSH

A Swift SSH framework that wraps libssh2.
MIT License
195 stars 75 forks source link

Q: How to send multiple commands in a row? #7

Closed tomaculum closed 6 years ago

tomaculum commented 6 years ago

Hi,

I want to implement a "today extension" and send ssh commands with a button from there.

Sometimes this button is pressed multiple times and there is a chance that the previous connection is not closed yet.

Is there any way to "intercept" the previous command and send it there? I have already tried following code but this did not work either:

if (command.connected && command.authenticated){
    command.execute(command) { (command, result: String?, error) in
        if let result = result {
        // ...
} else {
    // connect normally
}

Here is my file:

import UIKit
import NotificationCenter
import SwiftSH

class TodayViewController: UIViewController, NCWidgetProviding {

    var command: Command!
    @IBOutlet var button1: UIButton!

// ...

    override func viewDidLoad() {
        super.viewDidLoad()
        self.command = Command(host: "111.222.3.444", port: 22)
        // ...
    }

    @IBAction func button1Pressed(sender: UIButton){
        performCommand("pwd")
    }

    func performCommand(_ cmd: String) {
        print("send Command " + cmd)

        self.command
            .connect()
            .authenticate(.byPassword(username: "user", password: "pass"))
            .execute(cmd) { (cmd, result: String?, error) in
                if let result = result {
                    //...
                } else {
                    //...
                }
            }
    }
}
Frugghi commented 6 years ago

You could use a shell instead, you can send multiple commands on it without opening/closing it every time.

Or you can disable the button until you get a response/error for the previous command?