SwiftNIO SSH How to get answer?

Hello,

I am trying to implement the NIOSSHClient example into my iOS-app. At the moment, the connection with my own credentials is working and now I want to run commands.

For that, I replaced line 54 in simpleCLIParser with
return Result(commandString: "ls" ,target: URL(string: "ssh://mydomain.com")!, listen: nil)
but I don't get any answer in the terminal or anywhere else.
If I send "ls123", I see the error in terminal that the command doesn't exists, but I have no idea where this is generated so I can catch the answer, if the command is correct.

Can anybody give me a hint, how to receive the answer from the executed command so I can save and display it in other classes? I want to run multiple commands before closing the connection.

Best regards
Typo

The response data comes back in the channelRead function here. Right now this uses the NIO ChannelPipeline and the GlueHandler in the same module to glue the data into your terminal, but you can rewrite that function to do whatever you like.

Thank you for your answer. I already found that function but I don't get, where/ how the function should get called to receive the answer from the execution of the command as a string or something like that. Should I call it in channelActive, but where to get data of type NIOAny from?
As already mentioned: I can see errors in the console, if the executed command is wrong, but the answer isn't visible in the console.

The reason you can't see the text output in the console is likely because you're executing within Xcode. The sample program expects to be run from the terminal, where the console setup is different.

If you add breakpoints in the channelRead function as mentioned above, you should see the successful answer returned there.

Thank you.
Can you please give me also a hint how I can decide if I want to keep the connection open to send more then one command or if I want to close the connection immediately?

I can see that in the main class, we have the parse result with the one command, that I want to run but I have no idea, how run another command while the connection to the server is still open. I often receive an error in line 107 because wait() crashes. Maybe because I don't close the connection?

As already mentioned above, I am developing an iOS-app so it is a little bit tricky to use the CLI example but I did not found a better one so I am trying to build me basic commands like open connection(), close_connection(), send_something() -> String.

Thank you for your help.

Which precondition are you hitting?

To run a second command you need to create a new child channel and send a new exec request. That is, you can run these lines more than once: https://github.com/apple/swift-nio-ssh/blob/c995d144d2e0ba236ae3a2f6127d60fd199e3b4d/Sources/NIOSSHClient/main.swift#L99-L104.

Terms of Service

Privacy Policy

Cookie Policy