Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Open an SSH Session Channel
See more SSH Examples
Demonstrates the Chilkat Ssh.OpenSessionChannel method, which opens a new SSH session channel. It takes no arguments and returns the channel number, or -1 on failure. The transport must already be connected and authenticated.
Background: SSH multiplexes independent channels over one authenticated connection, and a session channel is the one used to run a command, start a shell, or invoke a subsystem. Opening the channel is only the container — nothing runs until you follow it with an
exec, shell, or subsystem request. Always test for a negative return rather than assuming success.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;
channelNum: Integer;
begin
success := False;
// Demonstrates the Ssh.OpenSessionChannel method, which opens a new SSH session channel. It
// takes no arguments and returns the channel number, or -1 on failure. The transport must
// already be connected and authenticated.
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 session channel. A negative return value indicates failure.
channelNum := ssh.OpenSessionChannel();
if (channelNum < 0) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
WriteLn('Opened session channel: ' + channelNum);
// Opening the channel does not start a shell or command -- send an exec, shell, or subsystem
// request next.
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.