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

Check if the SSH Connection is Alive

See more SSH Examples

Demonstrates the Chilkat Ssh.CheckConnection method, which reports whether the underlying TCP socket still appears to be open. It takes no arguments and returns true if the socket does not appear closed, or false if the connection is known to be closed or invalid. This example connects, authenticates, checks the socket, and then uses SendIgnore for a definitive liveness test.

Background: Unlike the IsConnected property, which simply reports the state the Ssh object already knows, CheckConnection performs an additional non-consuming check of the socket — it calls the operating system's recv with MSG_PEEK to inspect up to one byte without removing any data from the receive buffer. It is still a local check: it sends no SSH message, does not authenticate, and does not prove the remote server is responsive. A silently broken network path can keep it returning true until the OS notices, and a receive timeout, aborted operation, auth failure, or rejected channel-open does not necessarily close the socket. When you need an actual SSH round trip, use SendIgnore. Also note a false result may simply mean no connection exists, and this method may not update LastErrorText, so an existing error string could describe an earlier operation.

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;

begin
  success := False;

  //  Demonstrates the Ssh.CheckConnection method, which reports whether the underlying TCP socket
  //  still appears to be open.  It takes no arguments and returns True if the socket does not
  //  appear closed, or False if the connection is known to be closed or invalid.
  //  
  //  Unlike the IsConnected property, which reports the state already known to the Ssh object,
  //  CheckConnection performs an additional non-consuming peek at the socket.  It is still a local
  //  check: it does not send an SSH message or prove the remote server is responsive.  Use
  //  SendIgnore when an active SSH round trip is required.

  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;

  //  Check whether the socket still appears open.
  if (ssh.CheckConnection()) then
    begin
      WriteLn('The socket still appears open.');
    end
  else
    begin
      WriteLn('The connection is closed or invalid.');
    end;

  //  Note: a silently broken network path can still report open here until the OS detects it.
  //  For a definitive test that the SSH connection is writable, send an IGNORE message.
  success := ssh.SendIgnore();
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;
  WriteLn('SSH IGNORE sent -- the connection is writable.');

  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.