Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Enumerate Active SSH Channel Numbers
See more SSH Examples
Demonstrates the Chilkat Ssh.GetChannelNumber method, which returns the channel number at a zero-based enumeration index in the active channel collection. The only argument is the index — an enumeration position, not itself a channel number. Valid indexes run from 0 through NumOpenChannels - 1, and out-of-range indexes return -1.
Background: One SSH connection can carry many simultaneous channels, and this method lets you walk the live set rather than tracking every channel number yourself. The index-versus-channel-number distinction is the key point: index 0 is simply the first channel in the collection and may well be channel number 3. As channels open and close the enumeration shifts, so treat an index as valid only for the moment you read it.
Chilkat Pascal (Lazarus/Delphi) Downloads
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;
channelA: Integer;
channelB: Integer;
n: Integer;
i: Integer;
chNum: Integer;
begin
success := False;
// Demonstrates the Ssh.GetChannelNumber method, which returns the channel number at a
// zero-based enumeration index in the active channel collection. The only argument is the
// index, which is an enumeration position and not itself a channel number.
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;
// Open a couple of channels so the collection has more than one entry.
channelA := ssh.OpenSessionChannel();
if (channelA < 0) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
channelB := ssh.OpenSessionChannel();
if (channelB < 0) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
// Enumerate the active channels. Valid indexes are 0 through NumOpenChannels - 1.
n := ssh.NumOpenChannels;
WriteLn('Open channels: ' + n);
for i := 0 to n - 1 do
begin
chNum := ssh.GetChannelNumber(i);
WriteLn('Index ' + i + ' is channel number ' + chNum);
end;
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.