Sample code for 30+ languages & platforms
Delphi DLL

Connect an SSH Tunnel to the SSH Server

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.Connect method, which establishes the SSH connection that carries the tunnel's traffic. The first argument is the SSH server hostname and the second is the port. It performs the connection, SSH handshake, and host-key negotiation, but does not authenticate the SSH user.

Background: Connecting establishes only the SSH transport — the encrypted channel and host-key exchange. It is the point at which the server's identity becomes known, so it is where a security-conscious client checks HostKeyFingerprint against a trusted value before handing over credentials. Authentication and BeginAccepting follow; nothing is forwarded until they do.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
tunnel: HCkSshTunnel;
sshPort: Integer;
password: PWideChar;
waitForThreadExit: Boolean;

begin
success := False;

//  Demonstrates the SshTunnel.Connect method, which establishes the SSH connection that carries
//  tunneled traffic.  The 1st argument is the SSH server hostname and the 2nd is the port.  This
//  performs the connection, SSH handshake, and host-key negotiation, but does not authenticate.

tunnel := CkSshTunnel_Create();

//  The tunnel forwards accepted local connections to this destination through the SSH server.
CkSshTunnel_putDestHostname(tunnel,'db.internal.example.com');
CkSshTunnel_putDestPort(tunnel,5432);

//  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
//  does not authenticate yet.
sshPort := 22;
success := CkSshTunnel_Connect(tunnel,'ssh.example.com',sshPort);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSshTunnel__lastErrorText(tunnel));
    Exit;
  end;

//  Optionally verify the server's host key before authenticating.
Memo1.Lines.Add('Host key fingerprint: ' + CkSshTunnel__hostKeyFingerprint(tunnel));

//  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 := CkSshTunnel_AuthenticatePw(tunnel,'mySshLogin',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSshTunnel__lastErrorText(tunnel));
    Exit;
  end;

//  After authenticating, call BeginAccepting to start listening for local connections to forward.

waitForThreadExit := True;
success := CkSshTunnel_CloseTunnel(tunnel,waitForThreadExit);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSshTunnel__lastErrorText(tunnel));
    Exit;
  end;

CkSshTunnel_Dispose(tunnel);