Sample code for 30+ languages & platforms
Delphi ActiveX

Route Socket Connections through an SSH Object

See more Socket/SSL/TLS Examples

Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.

Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
ssh: TChilkatSsh;
sshPassword: WideString;
socket: TChilkatSocket;
bTls: Integer;
maxWaitMs: Integer;

begin
success := 0;

ssh := TChilkatSsh.Create(Self);

//  Connect and authenticate a standalone SSH object.
success := ssh.Connect('ssh.example.com',22);
if (success = 0) then
  begin
    Memo1.Lines.Add(ssh.LastErrorText);
    Exit;
  end;

//  The SSH password should come from a secure source rather than being hard-coded.
sshPassword := 'mySshPassword';
success := ssh.AuthenticatePw('sshUser',sshPassword);
if (success = 0) then
  begin
    Memo1.Lines.Add(ssh.LastErrorText);
    Exit;
  end;

socket := TChilkatSocket.Create(Self);

//  Route subsequent Connect calls through the already connected and authenticated SSH object.  The
//  destination is reached by SSH port forwarding rather than by a direct TCP connection.
success := socket.UseSsh(ssh.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(socket.LastErrorText);
    Exit;
  end;

//  Connect to the destination through the SSH tunnel.
bTls := 0;
maxWaitMs := 5000;
success := socket.Connect('db.internal',5432,bTls,maxWaitMs);
if (success = 0) then
  begin
    Memo1.Lines.Add(socket.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('Connected to the destination through the SSH tunnel.');