Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

SSH Execute Remote Commands

See more SSH Examples

Demonstrates running several commands on an SSH server using QuickCommand, which performs the entire open-channel, exec, receive, and retrieve sequence in a single call and returns the command's stdout.

Background: QuickCommand is the simplest way to run one-off commands: each call uses its own session channel on the same authenticated connection, so several commands can be issued in sequence without managing channels yourself. Because no PTY is involved the output is clean, with no prompt or echoed input to strip. Reach for the individual channel methods only when you need stderr separately, the exit status, or a long-lived interactive session.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Ssh;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ssh: TSsh;
  port: Integer;
  password: string;
  strOutput: string;

begin
  success := False;

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

  //  Demonstrates running several commands on an SSH server using QuickCommand, which handles the
  //  whole open-channel, exec, receive, and retrieve sequence in a single call.

  ssh := TSsh.Create;

  port := 22;
  success := ssh.Connect('ssh.example.com',port);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Normally you would not hard-code the password in source.  You should instead obtain it
  //  from an interactive prompt, environment variable, or a secrets vault.
  password := 'mySshPassword';

  success := ssh.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Each QuickCommand call runs one command on its own session channel and returns its stdout.
  strOutput := ssh.QuickCommand('df','utf-8');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn('---- df ----');
  WriteLn(strOutput);

  strOutput := ssh.QuickCommand('echo hello world','utf-8');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn('---- echo hello world ----');
  WriteLn(strOutput);

  strOutput := ssh.QuickCommand('date','utf-8');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn('---- date ----');
  WriteLn(strOutput);

  ssh.Disconnect();


  ssh.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.