jakeheis / Shout

SSH made easy in Swift
MIT License
360 stars 105 forks source link

Getting output during command execution #13

Closed dbuskariol closed 5 years ago

dbuskariol commented 6 years ago

I've managed to get Shout up and running :) I'm wondering if it's possible to get the output of a command as it's running, rather than the output at the end of the command being run. For example, the script I am running with Shout over SSH has output I'd like to show in my swift app. Currently it runs but will only print the whole output at once at the end of execution.

Is this possible?

jakeheis commented 6 years ago

Yes this is possible!

try ssh.execute("ls", output: { (chunk) in
   // do something with the output chunk
})
dbuskariol commented 6 years ago

Awesome, thanks for the reply @jakeheis! I tried using what you wrote, however I still have the same issue:

try ssh.execute(command, output: { (chunk) in
    print(chunk)
})

This will show nothing until the command finishes, then print everything at once. When I SSH in manually through terminal though, I see the output of the script as it happens.

jakeheis commented 6 years ago

Hm that's strange. I used this to test it:

import Shout

let ssh = try SSH(host: "mysite.com")
try ssh.authenticateByAgent(username: "user")

try ssh.execute("while true; do sleep 1; echo hello; done", output: { (chunk) in
    print(chunk, terminator: "")
})

This command would execute forever, but when I executed this program the output appeared line by line as it should. Shout seems to be calling the output closure correctly in my case, so I wonder if this is something unique to the command you are calling. Is it a standard unix command?

jakeheis commented 5 years ago

Closing, feel free to open with more information if need be