.. on Linux

Cocoa libssh2 wrapper

Posted by on Oct 24, 2011 in Apple, Xcode | 16 comments

I’ve modified a simple wrapper for the libssh2 library that now has the following functionality:

  • Code moved to separate classes to allow reusability
  • Multiple sessions to different servers can be achieved with a few lines of code
  • A Session can be passed to the operator class allowing operations (commands sent to it), more will be added
At the current time it connects fine to OSX and Linux sshd however I can’t connect to ESXi even with the correct password it reports incorrect, However I think I Can resolve this shortly.
Original wrapper (designed for iOS) can be found from http://lukehagan.com/ in his Git Repo.
Download here: SSH Wrapper
To use the wrapper add the files in the zip to your project, include the headers (ensure you’ve added the correct frameworks and the libssh2 library) etc. Example below:-

sshServer *server = [[sshServer alloc] init];

[server setSSHHost:@"192.168.2.100" port:22 user:@"dan" key:@"" keypub:@"" password:@"XXXXX"];

sshConnector *connection = [[sshConnector alloc] init];

[connection connect:server];

if ([server connectionStatus]) {

         NSLog(@”Server 1 connected”);

}

NSLog(@”%@”,[sshOperator execCommand:@"uname -a" sshServer:server]);

[connection closeSSH:server];

[connection release];

[server release];

 

:)

16 Comments

Join the conversation and post a comment.

  1. James

    will this work with executable files that aren’t builtin with mac or linux?

  2. dan

    What does that mean?

  3. Jan Galler

    I’m trying to use your classes and libssh2 with ARC, 64bit on Mac. But, I’ve several problems at the moment. Starts with: how to build libssh2 and include it? I’ve runned over all commands in INSTALL instructions, but where is the product located now? Beacause I dont know what to do, I tried it with the files from the original SSHCore. But of course, they’re compiled for iOS and not 64bit…
    Can you help me, maybe?

  4. dan

    Hallo Jan, I’ll look into it.. I’ve not done any dev for a while so not sure of any changes with xcode 4 / ARC and 64bit..

  5. Jan Galler

    Dear Dan,
    I’ve done it. Really ‘easy’, if you know who. Just run all instructions in the INSTALL file of libssh2. It looks for the target architecture. The dynamic lib you find ‘somewhere’, just include it. Doesnt work with ARC but that’s ok. I’ve found some parts here: http://goto11.net/how-to-bundle-a-c-library-with-a-cocoa-application/

  6. dan

    Ah fantastic, let me know if there is anything else :)

  7. Hashem

    Hi,

    Thanks for this, this was very helpful. When I’m executing your code, however, I get this:

    2012-09-12 19:28:57.237 xxxTest[12013:303] Connection Succesful
    2012-09-12 19:28:57.237 xxxTest[12013:303] Server 1 connected
    2012-09-12 19:28:57.401 xxxTest[12013:303] libssh2_channel_read returned -37
    2012-09-12 19:28:57.496 xxxTest[12013:303] libssh2_channel_read returned 0
    2012-09-12 19:28:57.497 xxxTest[12013:303]
    EXIT: 0 bytecount: 105
    2012-09-12 19:28:57.498 xxxTest[12013:303] (null)
    2012-09-12 19:28:57.498 xxxTest[12013:303] all done

    Any ideas on how I get the output of the command?

  8. Julian

    I tried using the wrapper in my iOS project.

    My attempt was to connect to a Mac OS X Lion server using password authentication.

    Every time I try to connect to the server I’m getting “Authentication by password failed for user”.

    Using the Terminal app, I don’t have problems to login to the server.

    How can I fix this issue?!
    Thanks

  9. Julian

    It’s me again.

    I did a little bit of research and found out that my SSH host does not support password authentication (configuration disallowed).
    I changed the configuration and now it works.

    BUT: I can’t change the configuration an my web server. So my question is: How does Terminal do that?
    I mean it can still authenticate with password when the option is disabled in configuration file.

    Thanks for help

  10. Jan Galler

    It’s me again! And I’ve a question about »NMSSH: Unable to open SCP session«…
    So, I’m doing the following:
    1. create a file
    2. download the file
    3. delete the file

    My solution works part by part, but I get »NMSSH: Unable to open SCP session« if I try to download the file after automatically generate it. If I do a manual download request, the file will be downloaded successfully! And I’m really sure the file paths are correct. The generate command works fine every time! The following link contains a screenshot of the download function: http://cl.ly/image/2U1d1L1U1q0g

    What could be the problem? Thank you so much!

  11. dan

    Can’t really help you here.. NMSSH is a wrapper written by someone else, that I wasn’t aware of.

  12. dan

    Julian.. the newer release of my sshwrapper supports password and keyboard authentication, so you should be fine now..

    -Dan

  13. Andi

    Hi,

    thank you for the awesome wrapper. I have only 1 problem. When i putt in the ip is working fine. If I request whit a dyndns.org its not working :/

    Thanks

  14. dan

    Download the release from GitHub I added code to do DNS resolution..

    -Dan

  15. Andi

    Hi,

    can’t find the updated files. Can you give me a link?

    Thanks

  16. t.o.

    Hey this lib works great for making calls through ssh UNLESS you need a return value.

    Context: Making calls to a bash script that returns a “/” separated list never responds consistently. Ended up reverting to NSTask and it works flawlessly (if you have ssh keys for the box)

    Where I was not relying on a response, everything that I called remotely with this lib also works flawlessly.

    Thanks again for the code!

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>