Sample code for 30+ languages & platforms
Delphi DLL

Check Whether an SSH Channel is Open

See more SSH Examples

Demonstrates the Chilkat Ssh.ChannelIsOpen method, which reports whether a channel number identifies a channel Chilkat currently considers open for application I/O. The only argument is the channel number. This example checks the channel before and after sending CLOSE.

Background: Because one SSH connection can carry many channels with independent lifetimes, code that manages several needs a way to ask whether a given one is still usable. This returns false for invalid or nonexistent channel numbers, after a local Disconnect, once remote closure is processed, and immediately after ChannelSendClose — which marks the channel locally closed without waiting for the remote side.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
ssh: HCkSsh;
sshPort: Integer;
password: PWideChar;
channelNum: Integer;

begin
success := False;

//  Demonstrates the Ssh.ChannelIsOpen method, which reports whether a channel number identifies
//  a channel Chilkat currently considers open for application I/O.  The only argument is the
//  channel number.

ssh := CkSsh_Create();

sshPort := 22;
success := CkSsh_Connect(ssh,'ssh.example.com',sshPort);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    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 := CkSsh_AuthenticatePw(ssh,'mySshLogin',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

//  Open a session channel.  A negative return value indicates failure.
channelNum := CkSsh_OpenSessionChannel(ssh);
if (channelNum < 0) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

if (CkSsh_ChannelIsOpen(ssh,channelNum)) then
  begin
    Memo1.Lines.Add('The channel is open.');
  end
else
  begin
    Memo1.Lines.Add('The channel is not open.');
  end;

//  Send CLOSE, which immediately marks the channel locally closed.
success := CkSsh_ChannelSendClose(ssh,channelNum);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

if (CkSsh_ChannelIsOpen(ssh,channelNum)) then
  begin
    Memo1.Lines.Add('Still open.');
  end
else
  begin
    Memo1.Lines.Add('The channel is now closed.');
  end;

CkSsh_Disconnect(ssh);

CkSsh_Dispose(ssh);