Sample code for 30+ languages & platforms
C

SCP Upload File

See more SCP Examples

Demonstrates how to upload a file using the SCP protocol (Secure Copy Protocol over SSH).

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkSsh ssh;
    const char *hostname;
    int port;
    HCkScp scp;
    const char *remotePath;
    const char *localPath;

    success = FALSE;

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

    ssh = CkSsh_Create();

    // Connect to an SSH server:

    // Hostname may be an IP address or hostname:
    hostname = "www.some-ssh-server.com";
    port = 22;

    success = CkSsh_Connect(ssh,hostname,port);
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        return;
    }

    // Wait a max of 5 seconds when reading responses..
    CkSsh_putIdleTimeoutMs(ssh,5000);

    // Authenticate using login/password:
    success = CkSsh_AuthenticatePw(ssh,"myLogin","myPassword");
    if (success != TRUE) {
        printf("%s\n",CkSsh_lastErrorText(ssh));
        CkSsh_Dispose(ssh);
        return;
    }

    // Once the SSH object is connected and authenticated, we use it
    // as the underlying transport in our SCP object.
    scp = CkScp_Create();

    success = CkScp_UseSsh(scp,ssh);
    if (success != TRUE) {
        printf("%s\n",CkScp_lastErrorText(scp));
        CkSsh_Dispose(ssh);
        CkScp_Dispose(scp);
        return;
    }

    remotePath = "test.txt";
    localPath = "/home/bob/test.txt";
    success = CkScp_UploadFile(scp,localPath,remotePath);
    if (success != TRUE) {
        printf("%s\n",CkScp_lastErrorText(scp));
        CkSsh_Dispose(ssh);
        CkScp_Dispose(scp);
        return;
    }

    printf("SCP upload file success.\n");

    // Disconnect
    CkSsh_Disconnect(ssh);


    CkSsh_Dispose(ssh);
    CkScp_Dispose(scp);

    }