Sample code for 30+ languages & platforms
DataFlex

Use a PuTTY Key for SSH Authentication

See more SSH Examples

Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private key. The key is imported with FromPuttyPrivateKey — setting Password first if it is encrypted — and passed to AuthenticatePk.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: A PuTTY key can be used directly for authentication with no conversion step, which is convenient when the key was generated by PuTTYgen on Windows. Conceptually the private key takes the place of a password while the username identifies the account, and the corresponding public key must already be installed on the server. Convert to OpenSSH format only if some other tool in your pipeline requires PEM.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Variant vPuttyKey
    Handle hoPuttyKey
    String sPpkText
    String sSshHostname
    Integer iSshPort
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private
    //  key.  The private key serves as the credential; the username identifies the account on the
    //  server.

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    //  LoadText is a convenience method that reads any text file into a string.  It does not itself
    //  load the key.
    Get Create (RefClass(cComChilkatSshKey)) To hoPuttyKey
    If (Not(IsComObjectCreated(hoPuttyKey))) Begin
        Send CreateComObject of hoPuttyKey
    End
    Get ComLoadText Of hoPuttyKey "qa_data/ppk/putty_private_secret.ppk" To sPpkText
    Get ComLastMethodSuccess Of hoPuttyKey To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoPuttyKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Set the password before importing an encrypted .ppk.  This should come from a secure source
    //  rather than being hard-coded.
    Set ComPassword Of hoPuttyKey To "myKeyPassword"

    Get ComFromPuttyPrivateKey Of hoPuttyKey sPpkText To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPuttyKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move "ssh.example.com" To sSshHostname
    Move 22 To iSshPort
    Get ComConnect Of hoSsh sSshHostname iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The corresponding public key must already be installed on the SSH server for the account.
    Get pvComObject of hoPuttyKey to vPuttyKey
    Get ComAuthenticatePk Of hoSsh "mySshLogin" vPuttyKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Connection and authentication with the SSH server completed."

    Send ComDisconnect To hoSsh


End_Procedure