Sample code for 30+ languages & platforms
Xbase++

Setting Environment Variables for SCP Transfers

See more SCP Examples

Demonstrates how to set remote environment variables for an SCP transfer.

Note 1: This example requires Chilkat v9.5.0.79 or greater.

Note 2: Setting environment variables for SCP is only supported by some SSH servers.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSsh
LOCAL oScp
LOCAL oJsonEnvVars
LOCAL cStrEnvVars
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")

//  First connect to an SSH server.
nSuccess := oSsh:Connect("example.com", 22)
IF (nSuccess != 1)
    ? oSsh:LastErrorText
    oSsh:destroy()
    RETURN
ENDIF

//  Wait a max of 5 seconds when reading responses..
oSsh:IdleTimeoutMs := 5000

//  Authenticate..
nSuccess := oSsh:AuthenticatePw("myLogin", "myPassword")
IF (nSuccess != 1)
    ? oSsh:LastErrorText
    oSsh:destroy()
    RETURN
ENDIF

//  After 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

//  Specify the environment variables to be set in JSON as follows.
//  This example sets two environment variables.  One is named "LCS_PASSWORD" and the other "MY_TEST_NAME".
oJsonEnvVars := CreateObject("Chilkat.JsonObject")
oJsonEnvVars:EmitCompact := 0
oJsonEnvVars:UpdateString("LCS_PASSWORD", "secret")
oJsonEnvVars:UpdateString("MY_TEST_NAME", "abc")
cStrEnvVars := oJsonEnvVars:Emit()

? cStrEnvVars

//  Setting the SendEnv property causes Chilkat to set each environment variable on the SSH server
//  prior to doing the upload or download.
oScp:SendEnv := cStrEnvVars

//  Do the upload..
cRemotePath := "starfish.jpg"
cLocalPath := "qa_data/jpg/starfish.jpg"
nSuccess := oScp:UploadFile(cLocalPath, cRemotePath)
IF (nSuccess != 1)
    ? oScp:LastErrorText
    oSsh:destroy()
    oScp:destroy()
    oJsonEnvVars:destroy()
    RETURN
ENDIF

? "SCP upload file success."

//  Disconnect
oSsh:Disconnect()

oSsh:destroy()
oScp:destroy()
oJsonEnvVars:destroy()