Sample code for 30+ languages & platforms
PureBasic

Multiple Hop SSH to SFTP

See more SFTP Examples

Demonstrates how to SFTP through an intermediate SSH server (multiple hop). The scheme looks like this:
Application => ServerSSH1 => ServerSFtp

The ConnectThroughSsh method is added in Chilkat version 9.5.0.55 to accomplish this task.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"
IncludeFile "CkSFtp.pb"

Procedure ChilkatExample()

    success.i = 0

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

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

    ; Hostname may be an IP address or domain name:
    hostname.s = "192.168.1.111"
    port.s = 22

    ; Connect directly to the 1st SSH server:
    success = CkSsh::ckConnect(ssh1,hostname,port)
    If success <> 1
        Debug CkSsh::ckLastErrorText(ssh1)
        CkSsh::ckDispose(ssh1)
        ProcedureReturn
    EndIf

    ; Authenticate using login/password:
    success = CkSsh::ckAuthenticatePw(ssh1,"myLogin","myPassword")
    If success <> 1
        Debug CkSsh::ckLastErrorText(ssh1)
        CkSsh::ckDispose(ssh1)
        ProcedureReturn
    EndIf

    ; Connect through the 1st SSH connection to reach a 2nd SSH server (for SFTP)
    ; Note: Any number of connections may be simultaneously tunneled through a single
    ; existing SSH connection.
    sftp.i = CkSFtp::ckCreate()
    If sftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to some SSH/SFTP server through ssh1.
    success = CkSFtp::ckConnectThroughSsh(sftp,ssh1,"sftp.someremoteserver.com",22)
    If success <> 1
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSsh::ckDispose(ssh1)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ; Authenticate with the SFTP server. 
    success = CkSFtp::ckAuthenticatePw(sftp,"myLogin","myPassword")
    If success <> 1
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSsh::ckDispose(ssh1)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ; After authenticating, the SFTP subsystem must be initialized:
    success = CkSFtp::ckInitializeSftp(sftp)
    If success <> 1
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSsh::ckDispose(ssh1)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ; Upload from the local file to the SSH server.
    ; Important -- the remote filepath is the 1st argument,
    ; the local filepath is the 2nd argument;
    remoteFilePath.s = "hamlet.xml"
    localFilePath.s = "c:/temp/hamlet.xml"

    success = CkSFtp::ckUploadFileByName(sftp,remoteFilePath,localFilePath)
    If success <> 1
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSsh::ckDispose(ssh1)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    Debug "Success."

    ; Close the connection with the SFTP server.(This closes the the tunnel through ssh1.)
    ; The connection with ssh1 is still alive, and may be used for more connections.
    CkSFtp::ckDisconnect(sftp)

    ; ...
    ; ...
    ; ...

    CkSsh::ckDisconnect(ssh1)


    CkSsh::ckDispose(ssh1)
    CkSFtp::ckDispose(sftp)


    ProcedureReturn
EndProcedure