Xbase++
Xbase++
SCP Upload to a Specific Remote Directory
See more SCP Examples
Demonstrates how to upload a file using the SCP protocol (Secure Copy Protocol over SSH). The file is uploaded to a specific remote directory. It can be either a path relative to the HOME directory of the SSH user account, or an absolute path on the remote filesystem. Obviously, in both cases the SSH user account must have permission to create or overwrite a file in the specified directory.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oSsh
LOCAL cHostname
LOCAL nPort
LOCAL oScp
LOCAL cRemotePath
LOCAL cLocalPath
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oSsh := CreateObject("Chilkat.Ssh")
// Connect to an SSH server:
// Hostname may be an IP address or hostname:
cHostname := "www.some-ssh-server.com"
nPort := 22
nSuccess := oSsh:Connect(cHostname, nPort)
IF (nSuccess != 1)
? oSsh:LastErrorText
oSsh:destroy()
RETURN
ENDIF
// Wait a max of 5 seconds when reading responses..
oSsh:IdleTimeoutMs := 5000
// Authenticate using login/password:
nSuccess := oSsh:AuthenticatePw("myLogin", "myPassword")
IF (nSuccess != 1)
? oSsh:LastErrorText
oSsh:destroy()
RETURN
ENDIF
// Once the SSH object is connected and authenticated, we use it
// as the underlying transport in our SCP object.
oScp := CreateObject("Chilkat.Scp")
nSuccess := oScp:UseSsh(oSsh)
IF (nSuccess != 1)
? oScp:LastErrorText
oSsh:destroy()
oScp:destroy()
RETURN
ENDIF
// This uploads a file to the "uploads/text" directory relative to the HOME
// directory of the SSH user account. For example, if the HOME directory is /home/chilkat,
// then this uploads to /home/chilkat/uploads/text/test.txt
// Note: The remote target directory must already exist on the SSH server.
cRemotePath := "uploads/text/test.txt"
cLocalPath := "/home/bob/test.txt"
nSuccess := oScp:UploadFile(cLocalPath, cRemotePath)
IF (nSuccess != 1)
? oScp:LastErrorText
oSsh:destroy()
oScp:destroy()
RETURN
ENDIF
// This upload fully specifies the absolute remote path.
cRemotePath := "/home/chilkat/junk/abc/hamlet.xml"
cLocalPath := "/home/bob/hamlet.xml"
nSuccess := oScp:UploadFile(cLocalPath, cRemotePath)
IF (nSuccess != 1)
? oScp:LastErrorText
oSsh:destroy()
oScp:destroy()
RETURN
ENDIF
? "SCP upload file success."
// Disconnect
oSsh:Disconnect()
oSsh:destroy()
oScp:destroy()