Sample code for 30+ languages & platforms
DataFlex

Authenticate an SSH Tunnel with a Private Key

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshAuthenticatePk, which authenticates an SSH tunnel session using a private key loaded into an SshKey object. Call SshOpenTunnel first.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The SSH server must have the corresponding public key authorized for the account. If the private key is encrypted, set the SshKey.Password property before loading it.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    Variant vSshKey
    Handle hoSshKey
    String sKeyText
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    Get ComSshOpenTunnel Of hoSocket "ssh.example.com" 22 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load the SSH private key.  The file path is relative to the application's current working
    //  directory.  If the key is encrypted, set the SshKey.Password property before calling
    //  FromOpenSshPrivateKey.
    Get Create (RefClass(cComChilkatSshKey)) To hoSshKey
    If (Not(IsComObjectCreated(hoSshKey))) Begin
        Send CreateComObject of hoSshKey
    End
    Get ComLoadText Of hoSshKey "qa_data/id_ed25519" To sKeyText
    Get ComLastMethodSuccess Of hoSshKey To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSshKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComFromOpenSshPrivateKey Of hoSshKey sKeyText To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSshKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Authenticate the SSH tunnel session using public-key authentication.  The SSH server must have the
    //  corresponding public key authorized for the account.
    Get pvComObject of hoSshKey to vSshKey
    Get ComSshAuthenticatePk Of hoSocket "sshUser" vSshKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "SSH public-key authentication succeeded."


End_Procedure