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

Receive from an SSH Channel Until a Pattern Matches

See more SSH Examples

Demonstrates the Chilkat Ssh.ChannelReceiveUntilMatch method, which receives text until the accumulated text matches a pattern. The arguments are the channel number, the pattern, the charset, and whether matching is case-sensitive. Existing buffered text is checked before waiting for more network data.

Important: Set the ReadTimeoutMs property before calling this method. It defaults to 0, meaning no limit, so without it the call waits forever if the received data never contains a match.

Background: Driving a shell channel means knowing when the remote side has finished talking. With no PTY requested the shell is non-interactive and never prints a prompt, so the reliable signal is a marker you deliberately echo after your command. This method automates "read until you see X." The pattern syntax is deliberately minimal: literal characters plus *, which matches zero or more characters and can span line breaks. ? and bracket classes are not supported, and backslash does not escape a literal *.

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.ChannelReceiveUntilMatch method, which receives text until the
  //  accumulated text matches a pattern.  The 1st argument is the channel number, the 2nd is the
  //  pattern, the 3rd is the charset, and the 4th selects case-sensitive matching.  The pattern
  //  supports literal characters plus "*", which matches zero or more characters.

  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;

  channelNum := ssh.OpenSessionChannel();
  if (channelNum < 0) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  Start a shell WITHOUT requesting a PTY.  Without a PTY the shell sees it is connected to a
  //  raw data pipe and enters non-interactive mode: it suppresses the command prompt, does not
  //  echo typed commands back, and skips interactive login scripts.  The output is therefore
  //  clean and easily parseable -- but because no prompt is ever sent, code must not wait for
  //  one.  Instead, have each command print a predictable marker, or wait for EOF / channel close.
  success := ssh.SendReqShell(channelNum);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  There is no prompt to wait for, so make the command itself announce when it has finished
  //  by echoing a marker after it.
  success := ssh.ChannelSendString(channelNum,'ls -l /tmp; 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;

  //  Receive until the marker appears in the output.
  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.