Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Scp, Ssh, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
ssh: HCkSsh;
scp: HCkScp;
jsonEnvVars: HCkJsonObject;
strEnvVars: PWideChar;
remotePath: PWideChar;
localPath: PWideChar;

begin
success := False;

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

ssh := CkSsh_Create();

// First connect to an SSH server.
success := CkSsh_Connect(ssh,'example.com',22);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

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

// Authenticate..
success := CkSsh_AuthenticatePw(ssh,'myLogin','myPassword');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

// After 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) then
  begin
    Memo1.Lines.Add(CkScp__lastErrorText(scp));
    Exit;
  end;

// 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".
jsonEnvVars := CkJsonObject_Create();
CkJsonObject_putEmitCompact(jsonEnvVars,False);
CkJsonObject_UpdateString(jsonEnvVars,'LCS_PASSWORD','secret');
CkJsonObject_UpdateString(jsonEnvVars,'MY_TEST_NAME','abc');
strEnvVars := CkJsonObject__emit(jsonEnvVars);

Memo1.Lines.Add(strEnvVars);

// Setting the SendEnv property causes Chilkat to set each environment variable on the SSH server
// prior to doing the upload or download.
CkScp_putSendEnv(scp,strEnvVars);

// Do the upload..
remotePath := 'starfish.jpg';
localPath := 'qa_data/jpg/starfish.jpg';
success := CkScp_UploadFile(scp,localPath,remotePath);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkScp__lastErrorText(scp));
    Exit;
  end;

Memo1.Lines.Add('SCP upload file success.');

// Disconnect
CkSsh_Disconnect(ssh);

CkSsh_Dispose(ssh);
CkScp_Dispose(scp);
CkJsonObject_Dispose(jsonEnvVars);

end;