Sample code for 30+ languages & platforms
C

SSH Public Key Authentication

See more SSH Examples

Demonstrates authenticating with an SSH server using public-key authentication. A private key is loaded into an SshKey object and passed to AuthenticatePk. The matching public key must already be installed on the server for the account.

Note: The private-key path is relative to the application's current working directory. Supply the path to your own key file.

Background: Public-key authentication is the standard for unattended and automated SSH: the private key never leaves the client, the server merely trusts the corresponding public key, and no reusable password crosses the wire. SshKey imports several formats — OpenSSH PEM keys via FromOpenSshPrivateKey and PuTTY .ppk files via FromPuttyPrivateKey — encrypted or not. For an encrypted key, set the Password property before importing. Note that LoadText is only a convenience for reading a text file into a string; it does not itself load the key.

Chilkat C Downloads

C
#include <C_CkSsh.h>
#include <C_CkSshKey.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSsh ssh;
    const char *hostname;
    int port;
    HCkSshKey key;
    const char *privKeyText;

    success = FALSE;

    //  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 public-key authentication.  The matching
    //  public key must already be installed on the SSH server for the account.

    ssh = CkSsh_Create();

    //  Timeouts, in milliseconds.
    CkSsh_putConnectTimeoutMs(ssh,5000);
    CkSsh_putReadTimeoutMs(ssh,15000);

    hostname = "ssh.example.com";
    port = 22;
    success = CkSsh_Connect(ssh,hostname,port);
    if (success == FALSE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        return;
    }

    //  LoadText is a convenience method that reads any text file into a string.  It does not itself
    //  load the key.
    key = CkSshKey_Create();
    privKeyText = CkSshKey_loadText(key,"qa_data/my_private_key.pem");
    if (CkSshKey_getLastMethodSuccess(key) == FALSE) {
        printf("%s\n",CkSshKey_lastErrorText(key));
        CkSsh_Dispose(ssh);
        CkSshKey_Dispose(key);
        return;
    }

    //  Import the private key.  Both OpenSSH and PuTTY formats are supported, encrypted or not.
    //  This example loads an unencrypted OpenSSH-format key; for a PuTTY .ppk file, call
    //  FromPuttyPrivateKey instead.  For an encrypted key, set key.Password before importing.
    success = CkSshKey_FromOpenSshPrivateKey(key,privKeyText);
    if (success == FALSE) {
        printf("%s\n",CkSshKey_lastErrorText(key));
        CkSsh_Dispose(ssh);
        CkSshKey_Dispose(key);
        return;
    }

    success = CkSsh_AuthenticatePk(ssh,"mySshLogin",key);
    if (success == FALSE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        CkSshKey_Dispose(key);
        return;
    }

    printf("Public-key authentication successful.\n");

    CkSsh_Disconnect(ssh);


    CkSsh_Dispose(ssh);
    CkSshKey_Dispose(key);

    }