Sample code for 30+ languages & platforms
Delphi DLL

Get the Type of an Active SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelType method, which returns the type of the channel at a zero-based enumeration index. The only argument is the index — an enumeration position, not a channel number. Values include session, x11, forwarded-tcpip, and direct-tcpip.

Background: Paired with GetChannelNumber, this turns the channel collection into a readable inventory of what a connection is currently doing — which channels are running commands (session) versus tunneling TCP traffic (direct-tcpip). That is valuable for diagnostics and for application code that manages several channel kinds at once. An out-of-range index causes the string-returning method to report failure.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
ssh: HCkSsh;
sshPort: Integer;
password: PWideChar;
channelNum: Integer;
n: Integer;
i: Integer;
chNum: Integer;
chType: PWideChar;

begin
success := False;

//  Demonstrates the Ssh.GetChannelType method, which returns the type of the channel at a
//  zero-based enumeration index.  The only argument is the index, which is an enumeration
//  position and not a channel number.  Values include "session" and "direct-tcpip".

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;

channelNum := CkSsh_OpenSessionChannel(ssh);
if (channelNum < 0) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

//  Enumerate the active channels and report each one's number and type.
n := CkSsh_getNumOpenChannels(ssh);

for i := 0 to n - 1 do
  begin
    chNum := CkSsh_GetChannelNumber(ssh,i);
    chType := CkSsh__getChannelType(ssh,i);
    if (CkSsh_getLastMethodSuccess(ssh) = False) then
      begin
        Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
        Exit;
      end;
    Memo1.Lines.Add('Channel ' + IntToStr(chNum) + ' is of type ' + chType);
  end;

CkSsh_Disconnect(ssh);

CkSsh_Dispose(ssh);