Delphi DLL
Delphi DLL
Connect an SSH Tunnel Through a Jump Host
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.ConnectThroughSsh method, which connects to an SSH server through an already connected and authenticated Ssh object. The first argument is the jump-host Ssh object, and the second and third are the destination SSH hostname and port.
Background: This chains the tunnel through a "jump host" (bastion): the application reaches a gateway server, then tunnels onward to an SSH server that is only accessible from inside the network. Each hop authenticates separately with its own credentials. The result is a normal
SshTunnel — still requiring its own authentication and BeginAccepting — whose transport is routed through the first connection.Chilkat Delphi DLL Downloads
var
success: Boolean;
sshJump: HCkSsh;
sshPort: Integer;
password: PWideChar;
tunnel: HCkSshTunnel;
waitForThreadExit: Boolean;
begin
success := False;
// Demonstrates the SshTunnel.ConnectThroughSsh method, which connects to an SSH server through
// an already connected and authenticated Ssh object (a jump host). The 1st argument is the Ssh
// object, and the 2nd and 3rd are the destination SSH hostname and port.
sshJump := CkSsh_Create();
// Connect and authenticate to the jump host first.
sshPort := 22;
success := CkSsh_Connect(sshJump,'jump.example.com',sshPort);
if (success = False) then
begin
Memo1.Lines.Add(CkSsh__lastErrorText(sshJump));
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(sshJump,'myJumpLogin',password);
if (success = False) then
begin
Memo1.Lines.Add(CkSsh__lastErrorText(sshJump));
Exit;
end;
// Set up the tunnel and connect to the target SSH server through the jump host.
tunnel := CkSshTunnel_Create();
CkSshTunnel_putDestHostname(tunnel,'db.internal.example.com');
CkSshTunnel_putDestPort(tunnel,5432);
success := CkSshTunnel_ConnectThroughSsh(tunnel,sshJump,'ssh.internal.example.com',sshPort);
if (success = False) then
begin
Memo1.Lines.Add(CkSshTunnel__lastErrorText(tunnel));
Exit;
end;
// Authenticate to the target SSH server (its own credentials).
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;
CkSsh_Disconnect(sshJump);
CkSsh_Dispose(sshJump);
CkSshTunnel_Dispose(tunnel);