Delphi DLL
Delphi DLL
Get an SSH Tunnel's Current State (Diagnostics)
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.GetCurrentState method, which returns an XML snapshot describing the tunnel manager, SSH channels, and current tunnel clients. It takes no arguments and is intended for diagnostics and monitoring.
Background: Because the tunnel forwards traffic on a background thread, there is no natural place in your code to observe what it is doing at a given moment.
GetCurrentState provides that visibility — byte counts, active channels, and connected clients — as structured XML suited to logging or a monitoring dashboard. It is a read-only snapshot and does not affect the tunnel.Chilkat Delphi DLL Downloads
var
success: Boolean;
tunnel: HCkSshTunnel;
sshPort: Integer;
password: PWideChar;
listenPort: Integer;
stateXml: PWideChar;
waitForThreadExit: Boolean;
begin
success := False;
// Demonstrates the SshTunnel.GetCurrentState method, which returns an XML snapshot describing the
// tunnel manager, SSH channels, and current tunnel clients. It takes no arguments and is
// intended for diagnostics and monitoring.
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;
// 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;
// Begin accepting so there is live tunnel state to inspect.
listenPort := 1080;
success := CkSshTunnel_BeginAccepting(tunnel,listenPort);
if (success = False) then
begin
Memo1.Lines.Add(CkSshTunnel__lastErrorText(tunnel));
Exit;
end;
// Get a diagnostic snapshot of the tunnel's current state as XML.
stateXml := CkSshTunnel__getCurrentState(tunnel);
if (CkSshTunnel_getLastMethodSuccess(tunnel) = False) then
begin
Memo1.Lines.Add(CkSshTunnel__lastErrorText(tunnel));
Exit;
end;
Memo1.Lines.Add(stateXml);
waitForThreadExit := True;
success := CkSshTunnel_CloseTunnel(tunnel,waitForThreadExit);
if (success = False) then
begin
Memo1.Lines.Add(CkSshTunnel__lastErrorText(tunnel));
Exit;
end;
CkSshTunnel_Dispose(tunnel);