Cocoa libssh2 wrapper

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];

 

:)

18 thoughts on “Cocoa libssh2 wrapper”

  1. 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?

  2. 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..

  3. 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?

  4. 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

  5. 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

  6. 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!

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

    -Dan

  8. 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

  9. 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!

  10. Good job. There is a very good tut/example, but how can i flush the output during a large bash script?
    When I execute a test script with a 5s loop, the App hangs 5 seconds and then the output of the last line is shown.

    Best regards
    Nelson

  11. Hello,
    I create correctly my frameworks and libssh2 library, but now how can I use this to my cocoa project to enable a ssh connection ?
    I add the SSH Wrapper into my project, but what I do with libssh2 to add it to my project ?

    Thank you :-)

Leave a Reply

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>