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

Start a Remote Shell with QuickShell

See more SSH Examples

Demonstrates the Chilkat Ssh.QuickShell method, which starts a remote shell using the simplified sequence OpenSessionChannelSendReqPtySendReqShell. It takes no arguments and returns the shell channel number, or -1 on failure. The default PTY size is 80 columns by 24 rows.

Background: Note that QuickShell does allocate a PTY, so unlike a bare SendReqShell the remote shell runs interactively: it prints a command prompt, echoes the commands you send, and runs interactive login scripts. That is convenient for terminal-style work but means the output contains prompts and echoed input that scripted parsing must tolerate. The method returns as soon as the shell request is accepted — it does not consume the initial login text or prompt, so read and discard that before relying on command output.

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;
  sshPort: Integer;
  password: string;
  channelNum: Integer;
  caseSensitive: Boolean;
  output: string;

begin
  success := False;

  //  Demonstrates the Ssh.QuickShell method, which starts a remote shell using the simplified
  //  sequence OpenSessionChannel, SendReqPty, and SendReqShell.  It takes no arguments and returns
  //  the shell channel number, or -1 on failure.  The default PTY size is 80 columns by 24 rows.

  ssh := TSsh.Create;

  sshPort := 22;
  success := ssh.Connect('ssh.example.com',sshPort);
  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;

  //  Start the shell.  Note that QuickShell allocates a PTY, so the remote shell runs
  //  interactively: it prints a command prompt and echoes the commands that are sent to it.
  channelNum := ssh.QuickShell();
  if (channelNum < 0) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  QuickShell returns as soon as the shell request is accepted -- it does not wait for or
  //  consume the initial login text or prompt.
  success := ssh.ChannelSendString(channelNum,'uname -a; echo DONE_MARKER' + #10,'utf-8');
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  IMPORTANT: Set a read timeout before receiving until a match.  ReadTimeoutMs defaults to 0,
  //  which means no limit -- without it, this call waits forever if the received data never
  //  contains a match.
  ssh.ReadTimeoutMs := 15000;

  caseSensitive := False;
  success := ssh.ChannelReceiveUntilMatch(channelNum,'DONE_MARKER','utf-8',caseSensitive);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  output := ssh.GetReceivedText(channelNum,'utf-8');
  if (ssh.LastMethodSuccess = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn(output);

  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.