Please note that the example is very basic and a lot of stuff need to be done after connecting. Please see official libssh guide https://api.libssh.org/master/libssh_tutor_guided_tour.html
          Last active
          December 26, 2024 17:41 
        
      - 
      
 - 
        
Save cy6erGn0m/134938242f28b27118039c038ea8c722 to your computer and use it in GitHub Desktop.  
    kotlin-native-libssh
  
        
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | plugins { | |
| kotlin("multiplatform") version "1.6.21" | |
| } | |
| group = "org.example" | |
| version = "1.0-SNAPSHOT" | |
| repositories { | |
| mavenCentral() | |
| } | |
| kotlin { | |
| val hostOs = System.getProperty("os.name") | |
| val isMingwX64 = hostOs.startsWith("Windows") | |
| val nativeTarget = when { | |
| hostOs == "Mac OS X" -> macosX64("native") | |
| hostOs == "Linux" -> linuxX64("native") | |
| isMingwX64 -> mingwX64("native") | |
| else -> throw GradleException("Host OS is not supported in Kotlin/Native.") | |
| } | |
| nativeTarget.apply { | |
| compilations["main"].cinterops { | |
| val libssh by creating { | |
| defFile("src/nativeMain/interop/ssh.def") | |
| } | |
| } | |
| binaries { | |
| executable { | |
| entryPoint = "main" | |
| } | |
| } | |
| } | |
| sourceSets { | |
| val nativeMain by getting | |
| val nativeTest by getting | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import kotlinx.cinterop.* | |
| import org.ssh.* | |
| fun main(): Unit = memScoped { | |
| val session = ssh_new() ?: return | |
| val port = alloc<IntVar>() | |
| port.value = 22 | |
| val verbosity = alloc<UIntVar>() | |
| verbosity.value = SSH_LOG_PROTOCOL | |
| try { | |
| ssh_options_set(session, ssh_options_e.SSH_OPTIONS_HOST, "host".utf8.getPointer(this)) | |
| ssh_options_set(session, ssh_options_e.SSH_OPTIONS_PORT, port.ptr) | |
| ssh_options_set(session, ssh_options_e.SSH_OPTIONS_LOG_VERBOSITY, verbosity.ptr) | |
| val rc = ssh_connect(session) | |
| if (rc == SSH_OK) { | |
| println("Connected successfully") | |
| ssh_disconnect(session) | |
| } else { | |
| println("Failed to connect: ${ssh_get_error(session)?.toKStringFromUtf8()}") | |
| } | |
| } finally { | |
| ssh_free(session) | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | headers = libssh/libssh.h | |
| headerFilter = libssh/* | |
| package = org.ssh | |
| compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu | |
| linkerOpts.linux = -L/usr/lib/x86_64-linux-gnu -lssh | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment