Sample code for 30+ languages & platforms
PureBasic

Authenticate an SSH Tunnel with a Private Key

See more POP3 Examples

Demonstrates the Chilkat MailMan.SshAuthenticatePk method, which authenticates with the SSH server using public-key authentication after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the SSH private key (an SshKey object). This example opens a tunnel, loads an OpenSSH private key, authenticates with it, and runs a POP3 operation through the tunnel.

Background: SSH public-key authentication replaces a password with a key pair: the public key is installed on the SSH server, and the client proves it holds the matching private key without ever transmitting a secret. This is the preferred method for automated systems — there is no password to embed or rotate, and the key can be revoked server-side. Chilkat reads the key with an SshKey object, which understands common formats such as OpenSSH and PuTTY.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMailMan.pb"
IncludeFile "CkSshKey.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the MailMan.SshAuthenticatePk method, which authenticates with the SSH server
    ;  using public-key authentication after an SSH tunnel has been opened.  The 1st argument is
    ;  the SSH account name and the 2nd is the SSH private key.

    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Configure the POP3 server connection.  These connections will travel through the tunnel.
    CkMailMan::setCkMailHost(mailman, "pop.example.com")
    CkMailMan::setCkMailPort(mailman, 995)
    CkMailMan::setCkPopSsl(mailman, 1)
    CkMailMan::setCkPopUsername(mailman, "user@example.com")
    CkMailMan::setCkPopPassword(mailman, "myPassword")

    ;  Open the SSH tunnel first.
    success = CkMailMan::ckSshOpenTunnel(mailman,"ssh.example.com",22)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    ;  Load the SSH private key from an OpenSSH-format key file.
    sshKey.i = CkSshKey::ckCreate()
    If sshKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    keyText.s = CkSshKey::ckLoadText(sshKey,"qa_data/ssh/id_rsa")
    If CkSshKey::ckLastMethodSuccess(sshKey) = 0
        Debug CkSshKey::ckLastErrorText(sshKey)
        CkMailMan::ckDispose(mailman)
        CkSshKey::ckDispose(sshKey)
        ProcedureReturn
    EndIf

    success = CkSshKey::ckFromOpenSshPrivateKey(sshKey,keyText)
    If success = 0
        Debug CkSshKey::ckLastErrorText(sshKey)
        CkMailMan::ckDispose(mailman)
        CkSshKey::ckDispose(sshKey)
        ProcedureReturn
    EndIf

    ;  Authenticate with the SSH server using the private key.
    success = CkMailMan::ckSshAuthenticatePk(mailman,"sshUser",sshKey)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        CkSshKey::ckDispose(sshKey)
        ProcedureReturn
    EndIf

    ;  POP3 operations now travel through the SSH tunnel.
    count.i = CkMailMan::ckGetMailboxCount(mailman)
    If count < 0
        Debug "Failed to get the mailbox count."
    Else
        Debug "Mailbox count: " + Str(count)
    EndIf

    success = CkMailMan::ckSshCloseTunnel(mailman)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        CkSshKey::ckDispose(sshKey)
        ProcedureReturn
    EndIf

    ;  Note: The path "qa_data/ssh/id_rsa" is a relative local filesystem path,
    ;  relative to the current working directory of the running application.


    CkMailMan::ckDispose(mailman)
    CkSshKey::ckDispose(sshKey)


    ProcedureReturn
EndProcedure